11

I would like to remove a file from a folder in PHP, but I just have the path to this file, would it be ok to give the path to unlink? For example

unlink('path/to/file.txt');

If this doesn't work, the only way to get rid of those files would be to create a .php file in the path/to/ directory and include it somehow in my file an call a method there to remove the file, right?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Masiar
  • 20,450
  • 31
  • 97
  • 140

9 Answers9

22

Got an easy method for your question

Use this code to remove a file from a folder

$_SERVER['DOCUMENT_ROOT']

this can be used inside the unlink function

worked code

     unlink($_SERVER['DOCUMENT_ROOT'] . "/path/to/file.txt");
Nikz
  • 1,346
  • 1
  • 18
  • 24
7

Have a look at the unlink documentation:

bool unlink ( string $filename [, resource $context ] )

and

filename
Path to the file.

So it only takes a string as filename.

Make sure the file is reachable with the path from the location you execute the script. This is not a problem with absolute paths, but you might have one with relative paths.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
5

unlink works fine with paths.

Description bool unlink ( string $filename [, resource $context ] )

Deletes filename. Similar to the Unix C unlink() function. A E_WARNING level error will be generated on failure.

filename

Path to the file.

In case had a problem with the permissions denied error, it's sometimes caused when you try to delete a file that's in a folder higher in the hierarchy to your working directory (i.e. when trying to delete a path that starts with "../").

So to work around this problem, you can use chdir() to change the working directory to the folder where the file you want to unlink is located.

<?php
    $old = getcwd(); // Save the current directory
    chdir($path_to_file);
    unlink($filename);
    chdir($old); // Restore the old working directory   
?>
ayush
  • 14,350
  • 11
  • 53
  • 100
3

Don't forget to check if the file exists, or you will get an error if it doesn't:

$file_with_path = $_SERVER['DOCUMENT_ROOT'] . "/path/to/file.txt";
if (file_exists($file_with_path)) {
  unlink($file_with_path);
}
Nag Hammadi
  • 297
  • 1
  • 6
  • 17
1

You CAN use unlink with a path.

You can also perform unlink on a directory, as long as you have emptied it first.

Here is the manual: http://php.net/manual/en/function.unlink.php

Fenton
  • 241,084
  • 71
  • 387
  • 401
0

We can use this code

$path="images/all11.css";

if(unlink($path)) echo "Deleted file ";
0

According to the documentation, unlink accepts string parameter for the path.

http://php.net/manual/en/function.unlink.php

In other words... you have what you need to delete the file.

Chris Baker
  • 49,926
  • 12
  • 96
  • 115
0

Not only is it OK, it is the only way to delete a file in PHP (besides system calls).

John Cartwright
  • 5,109
  • 22
  • 25
-4
if (isset($_POST['remove_file'])) {
           $file_path=$_POST['fileremove'];
     // chown($file_path, 'asif');
     // echo $file_path;
    if (file_exists($file_path)) {
          unlink($file_path);
        echo "file deleted<br> the name of file is".$file_path."";

        # code...
    }
    else
        echo "file is not deleted ".$file_path."";
    # code...
}