如何在 PHP 中递归删除目录及其全部内容(文件和子目录)?
如何在 PHP 中删除目录及其全部内容(文件和子目录)?
How do I delete a directory and its entire contents (files and subdirectories) in PHP?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(21)
rmdir
手册页中的用户贡献部分包含一个不错的实现:The user-contributed section in the manual page of
rmdir
contains a decent implementation:构建于 Pixel 开发人员的评论,使用 SPL 的代码片段可能如下所示:
注意:它不进行健全性检查,并使用 PHP 5.3 中 FilesystemIterator 引入的 SKIP_DOTS 标志。 0。当然,
$todo
可以是if
/else
。重要的一点是,CHILD_FIRST
用于首先迭代子级(文件),然后再迭代其父级(文件夹)。Building on The Pixel Developer's comment, a snippet using the SPL might look like:
Note: It does no sanity checking and makes use of the SKIP_DOTS flag introduced with the FilesystemIterator in PHP 5.3.0. Of course, the
$todo
could be anif
/else
. The important point is thatCHILD_FIRST
is used to iterate over the children (files) first before their parent (folders).删除路径中的所有文件和文件夹。
Deletes all files and folders in the path.
对于 *nix,您可以使用
shell_exec
(对于rm -R
)或DEL /Sfolder_name
(对于 Windows)。For *nix you can use a
shell_exec
forrm -R
orDEL /S folder_name
for Windows.如果您使用 Yii,那么您可以将其留给框架:
If you are using Yii then you can leave it to the framework:
100% 有效的解决方案
The 100% working solution
使用 glob() 函数的示例。它将递归删除所有文件和文件夹,包括以点开头的文件。
Example with glob() function. It will delete all files and folders recursively, including files that starts with dot.
增强 @Artefacto 的解决方案 - 更正了拼写错误并简化了代码,适用于两者 - 空 &&非空目录。
Enhanced @Artefacto 's solution - corrected typos and simplified code, working for both - empty && non-empty directories .
正确使用 DirectoryIterator 和递归:
Using DirectoryIterator and recursion correctly:
似乎所有其他答案都假设给函数的路径始终是一个目录。此变体适用于删除目录以及单个文件:
编辑:如果您很挑剔(而且您应该很挑剔),您可能需要添加代码来检查
scandir()
、rmdir ()
和unlink()
返回错误并抛出异常(如果是)。It seems that all other answers assume the path given to the function is always a directory. This variant works to remove directories as well as single files:
EDIT: If you're picky (and you should be picky) you may want to add code to check for
scandir()
,rmdir()
andunlink()
returning an error and throw an exception if so.像这样的东西吗?
Something like this?
“简单”的代码可以工作并且可以被十岁的孩子阅读:
请注意,我所做的只是扩展/简化和修复(不适用于非空目录)这里的解决方案:
在 PHP 中,如何递归删除所有非空文件夹?
'simple' code that works and can be read by a ten year old:
Please note that all I did was expand/simplify and fix (didn't work for non empty dir) the solution here:
In PHP how do I recursively remove all folders that aren't empty?
unlinkr 函数通过确保它不会删除脚本本身来递归删除给定路径中的所有文件夹和文件。
如果您想删除放置此脚本的所有文件和文件夹,请按如下方式调用
如果您只想删除 php 文件,则按如下方式调用
您也可以使用任何其他路径来删除文件
这将删除所有文件在 home/user/temp 目录中。
unlinkr function recursively deletes all the folders and files in given path by making sure it doesn't delete the script itself.
if you want to delete all files and folders where you place this script then call it as following
if you want to just delete just php files then call it as following
you can use any other path to delete the files as well
This will delete all files in home/user/temp directory.
我使用这个代码...
或者这个...
I use this code ...
or this one...
我刚刚根据 StackOverflow 的一些讨论编写了这段代码。我还没有在Linux环境下进行测试。它是为了完全删除文件或目录而创建的:
I juste made this code, from some StackOverflow discussions. I didn't test on Linux environment yet. It is made in order to delete a file or a directory, completely :
@XzaR 解决方案的修改变体。当从空文件夹中删除所有文件时,它会删除空文件夹,并抛出异常,而不是在错误时返回 false。
Modified variant of @XzaR solution. It does remove empty folders, when all files are deleted from them and it throws exceptions instead of returning false on errors.
运行完测试后,只需从类中的 #unlink 和 #rmdir 中删除 # 即可。
Once you finish running tests , just remove # from the #unlink and #rmdir in the class .
cleanup.php:
cleanup.php :