开发者

PHP does unlink function works with a path?

开发者 https://www.devze.com 2023-02-10 10:07 出处:网络
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

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?


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");


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.


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   
?>


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);
}


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


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.


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


We can use this code

$path="images/all11.css";

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


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...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号