php 如何通过PHP删除文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2371408/
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
How to delete a file via PHP?
提问by Ken
How do I delete a file from my server with PHP if the file is in another directory?
如果文件位于另一个目录中,如何使用 PHP 从我的服务器中删除文件?
Here is my page layout:
这是我的页面布局:
projects/backend/removeProjectData.php(this file deletes all my entries for the database and should also delete the related file)public_files/22.pdf(the place where the file is located.)
projects/backend/removeProjectData.php(这个文件删除了我所有的数据库条目,也应该删除相关文件)public_files/22.pdf(文件所在的位置。)
I'm using the unlinkfunction:
我正在使用该unlink功能:
unlink('../../public_files/' . $fileName);
unlink('../../public_files/' . $fileName);
But this always gives me an error that the file does not exist. Any ideas?
但这总是给我一个错误,即该文件不存在。有任何想法吗?
回答by Gordon
The following should help
以下应该有所帮助
realpath— Returns canonicalized absolute pathnameis_writable— Tells whether the filename is writableunlink— Deletes a file
realpath— 返回规范化的绝对路径名is_writable— 判断文件名是否可写unlink— 删除文件
Run your filepath through realpath, then check if the returned path is writable and if so, unlink it.
通过 realpath 运行文件路径,然后检查返回的路径是否可写,如果是,则取消链接。
回答by UbiQue
$files = [
'./first.jpg',
'./second.jpg',
'./third.jpg'
];
foreach ($files as $file) {
if (file_exists($file)) {
unlink($file);
} else {
// File not found.
}
}
回答by richsage
Check your permissions first of all on the file, to make sure you can a) see it from your script, and b) are able to delete it.
首先检查您对文件的权限,以确保您可以 a) 从脚本中看到它,并且 b) 能够删除它。
You can also use a path calculated from the directory you're currently running the script in, eg:
您还可以使用从当前运行脚本的目录计算出的路径,例如:
unlink(dirname(__FILE__) . "/../../public_files/" . $filename);
(in PHP 5.3 I believe you can use the __DIR__constant instead of dirname()but I've not used it myself yet)
(在 PHP 5.3 中,我相信您可以使用__DIR__常量代替,dirname()但我自己还没有使用过)
回答by ankit suthar
You can delete the file using
您可以使用删除文件
unlink($Your_file_path);
but if you are deleting a file from it's http path then this unlink is not work proper. You have to give a file path correct.
但是,如果您要从其 http 路径中删除文件,则此取消链接将无法正常工作。您必须提供正确的文件路径。
回答by Junaid Atari
AIO solution, handles everything, It's not my work but I just improved myself. Enjoy!
AIO 解决方案,处理一切,这不是我的工作,我只是提高了自己。享受!
/**
* Unlink a file, which handles symlinks.
* @see https://github.com/luyadev/luya/blob/master/core/helpers/FileHelper.php
* @param string $filename The file path to the file to delete.
* @return boolean Whether the file has been removed or not.
*/
function unlinkFile ( $filename ) {
// try to force symlinks
if ( is_link ($filename) ) {
$sym = @readlink ($filename);
if ( $sym ) {
return is_writable ($filename) && @unlink ($filename);
}
}
// try to use real path
if ( realpath ($filename) && realpath ($filename) !== $filename ) {
return is_writable ($filename) && @unlink (realpath ($filename));
}
// default unlink
return is_writable ($filename) && @unlink ($filename);
}
回答by abetwothree
I know this question is a bit old, but this is something simple that works for me very well to delete images off my project I'm working on.
我知道这个问题有点老了,但这是一个简单的方法,非常适合从我正在处理的项目中删除图像。
unlink(dirname(__FILE__) . "/img/tasks/" . 'image.jpg');
The dirname(__FILE__)section prints out the base path to your project. The /img/tasks/are two folders down from my base path. And finally, there's my image I want to delete which you can make into anything you need to.
该dirname(__FILE__)部分打印出项目的基本路径。这/img/tasks/是我的基本路径下的两个文件夹。最后,我想删除我的图像,你可以把它做成任何你需要的东西。
With this I have not had any problem getting to my files on my server and deleting them.
有了这个,我在我的服务器上访问我的文件并删除它们没有任何问题。

