PHP does unlink function works with a path?

微笑、不失礼 提交于 2019-12-08 16:08:13

问题


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?


回答1:


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.




回答2:


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



回答3:


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



回答4:


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




回答5:


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



回答6:


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.




回答7:


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




回答8:


We can use this code

$path="images/all11.css";

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



回答9:


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


来源:https://stackoverflow.com/questions/5006569/php-does-unlink-function-works-with-a-path

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!