PHP does unlink function works with a path?
Asked Answered
S

9

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?

Sulamith answered 15/2, 2011 at 16:41 Comment(0)
S
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.

Starchy answered 15/2, 2011 at 16:46 Comment(0)
P
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");
Plasmagel answered 13/7, 2013 at 6:5 Comment(0)
S
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.

Starchy answered 15/2, 2011 at 16:46 Comment(0)
D
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   
?>
Dancette answered 15/2, 2011 at 16:49 Comment(0)
D
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);
}
Decidua answered 18/8, 2013 at 11:18 Comment(0)
A
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

Avrom answered 15/2, 2011 at 16:45 Comment(1)
Unlinking a directory doesn't work on all filesystems, which is why there's rmdir()Cp
S
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.

Sudhir answered 15/2, 2011 at 16:46 Comment(0)
O
0

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

Origen answered 15/2, 2011 at 16:46 Comment(0)
P
0

We can use this code

$path="images/all11.css";

if(unlink($path)) echo "Deleted file ";
Palmirapalmistry answered 12/10, 2016 at 6:22 Comment(0)
P
-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...
}
Phyla answered 2/2, 2017 at 10:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.