php 如何使用php从目录中删除文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13338851/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:11:47  来源:igfitidea点击:

how to delete a file from a directory using php

php

提问by Jon

From the title you can see i am looking for a way to delete a file from a different directory. All i can find on the subject is the unlink(), but from what i read in the documentation and from testing that function is it deletes the file name from the code you put it in. Makes me think it's quite similar to closing the file. What I am trying to do is actually delete a file using code so my user doesn't have to manually go to the folder and find the song they just deleted from the mysql database.

从标题中您可以看到我正在寻找一种从不同目录中删除文件的方法。我能在这个主题上找到的只是 unlink(),但是从我在文档中读到的内容和测试该函数的内容是,它会从您放入的代码中删除文件名。让我觉得这与关闭文件非常相似. 我想要做的实际上是使用代码删除文件,这样我的用户就不必手动转到文件夹并找到他们刚刚从 mysql 数据库中删除的歌曲。

回答by Samuel Cook

unlink()will delete the file off your server

unlink()将从您的服务器上删除文件

rmdir()will delete a directory off your server

rmdir()将从您的服务器上删除一个目录

Note: Once it's gone, it's gone.

注意:一旦它消失了,它就消失了。

回答by emartel

unlinktruly deletes the specified file from the disk

unlink真正从磁盘中删除指定的文件

回答by andy

We can delete files by giving its URL or path in PHP by using unlink command. This command will work only if write permission is given to the folder or file. Without this the delete command will fail. Here is the command to delete the file.

我们可以通过使用 unlink 命令在 PHP 中给出其 URL 或路径来删除文件。仅当对文件夹或文件授予写权限时,此命令才有效。如果没有这个,删除命令将失败。这是删除文件的命令。

$path="images/all11.css";
if(unlink($path)) echo "Deleted file ";

回答by Aryan

  • realpath— Returns canonicalized absolute pathname
  • is_readable— Tells whether a file exists and is readable
  • unlink— Deletes a file

Run your filepath through realpath, then check if the returned path exists and if so, unlink it.

通过 realpath 运行文件路径,然后检查返回的路径是否存在,如果存在,则取消链接。