如何删除不为空的目录?
我尝试使用 rmdir 删除目录,但收到“目录不为空”消息,因为其中仍有文件。
我可以使用什么函数来删除包含所有文件的目录?
I am trying to remove a directory with rmdir
, but I received the 'Directory not empty' message, because it still has files in it.
What function can I use to remove a directory with all the files in it as well?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
想不出比这更简单、更有效的方法了
Can't think of an easier and more efficient way to do that than this
您始终可以尝试使用系统命令。
如果在 Linux 上使用:
rm -rf /dir
如果在 Windows 上使用:
rd c:\dir /S /Q
在上面的帖子中 (John Kugelman)我想 PHP 解析器会优化 foreach 中的 scandir 但对我来说使用
似乎是错误的
。foreach
条件语句中的 scandir您还可以只执行两个
array_shift
命令来删除.
和..
,而不是始终在循环中进行检查。You could always try to use system commands.
If on linux use:
rm -rf /dir
If on windows use:
rd c:\dir /S /Q
In the post above (John Kugelman) I suppose the PHP parser will optimize that scandir in the foreach but it just seems wrong to me to have the
scandir
in theforeach
condition statement.You could also just do two
array_shift
commands to remove the.
and..
instead of always checking in the loop.我的案例有很多棘手的目录(名称包含特殊字符、深层嵌套等)和隐藏文件,这些文件与其他建议的解决方案一起产生“目录不为空”错误。由于仅 Unix 的解决方案是不可接受的,因此我进行了测试,直到得出以下解决方案(在我的情况下效果很好):
My case had quite a few tricky directories (names containing special characters, deep nesting, etc) and hidden files that produced "Directory not empty" errors with other suggested solutions. Since a Unix-only solution was unacceptable, I tested until I arrived at the following solution (which worked well in my case):
这是我使用的:
Here what I used:
已经有很多解决方案,还有另一种可能性,使用 PHP 箭头函数使用更少的代码:
There are plenty of solutions already, still another possibility with less code using PHP arrow function:
来自 http://php.net/manual/en/function.rmdir.php #91797
Glob 函数不会返回隐藏文件,因此在尝试递归删除树时,scandir 可能更有用。
From http://php.net/manual/en/function.rmdir.php#91797
Glob function doesn't return the hidden files, therefore scandir can be more useful, when trying to delete recursively a tree.
使用此功能,您将能够删除任何文件或文件夹
With this function, you will be able to delete any file or folder
我没有到达删除文件夹,因为 PHP 告诉我它不是空的。但确实如此。 Naman 的功能是完成我的功能的好解决方案。这就是我使用的:
因此,要删除目录并递归地删除其内容:
I didn't arrive to delete a folder because PHP said me it was not empty. But it was. The function by Naman was the good solution to complete mine. So this is what I use :
So, to delete a directory and recursively its content :
尝试以下方便的功能:
Try the following handy function:
没有内置函数可以执行此操作,但请参阅 https://www.php 底部的注释.net/rmdir。许多评论者发布了他们自己的递归目录删除功能。您可以从中选择。
这是看起来不错的:
你可以调用
rm -rf
如果你想让事情变得简单。这确实会使您的脚本仅适用于 UNIX,因此请注意这一点。如果你走那条路我会尝试这样的事情:There is no built-in function to do this, but see the comments at the bottom of https://www.php.net/rmdir. A number of commenters posted their own recursive directory deletion functions. You can take your pick from those.
Here's one that looks decent:
You could just invoke
rm -rf
if you want to keep things simple. That does make your script UNIX-only, so beware of that. If you go that route I would try something like: