PHP unlink 函数是否适用于路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5006569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP does unlink function works with a path?
提问by Masiar
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
我想从 PHP 的文件夹中删除一个文件,但我只有这个文件的路径,是否可以提供取消链接的路径?例如
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?
如果这不起作用,摆脱这些文件的唯一方法是在 path/to/ 目录中创建一个 .php 文件,并以某种方式将它包含在我的文件中,然后调用那里的方法来删除文件,对吗?
采纳答案by Felix Kling
Have a look at the unlink
documentation:
看看unlink
文档:
bool unlink ( string $filename [, resource $context ] )
bool unlink ( string $filename [, resource $context ] )
and
和
filename
Path to the file.
文件名 文件的
路径。
So it onlytakes 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.
确保通过执行脚本的位置的路径可以访问该文件。这不是绝对路径的问题,但您可能有相对路径的问题。
回答by Nikz
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
这可以在 unlink 函数中使用
worked code
工作代码
unlink($_SERVER['DOCUMENT_ROOT'] . "/path/to/file.txt");
回答by ayush
unlink works fine with paths.
unlink 适用于路径。
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.
描述 bool unlink ( string $filename [, resource $context ] )
删除文件名。类似于 Unix C unlink() 函数。失败时将生成 E_WARNING 级别错误。
文档名称
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.
因此,要解决此问题,您可以使用 chdir() 将工作目录更改为要取消链接的文件所在的文件夹。
<?php
$old = getcwd(); // Save the current directory
chdir($path_to_file);
unlink($filename);
chdir($old); // Restore the old working directory
?>
回答by Nag Hammadi
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);
}
回答by Fenton
You CAN use unlink with a path.
您可以将 unlink 与路径一起使用。
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
回答by Chris Baker
According to the documentation, unlink
accepts string parameter for the path.
根据文档,unlink
接受路径的字符串参数。
http://php.net/manual/en/function.unlink.php
http://php.net/manual/en/function.unlink.php
In other words... you have what you need to delete the file.
换句话说......你有你需要的东西来删除文件。
回答by John Cartwright
Not only is it OK, it is the only way to delete a file in PHP (besides system calls).
不仅可以,而且是 PHP 中删除文件的唯一方法(除了系统调用)。
回答by John Cartwright
We can use this code
我们可以使用这个代码
$path="images/all11.css";
if(unlink($path)) echo "Deleted file ";
回答by asif rayyan
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...
}