删除其中包含文件的目录?
我想知道,删除包含所有文件的目录的最简单方法是什么?
我正在使用 rmdir(PATH . '/' . $value); 来删除文件夹,但是,如果其中有文件,我根本无法删除它。
I wonder, what's the easiest way to delete a directory with all its files in it?
I'm using rmdir(PATH . '/' . $value);
to delete a folder, however, if there are files inside of it, I simply can't delete it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
如今至少有两种选择。
在删除文件夹之前,请删除其所有文件和文件夹(这意味着递归!)。这是一个例子:
如果您使用的是 5.2+,您可以使用 RecursiveIterator 来完成此操作,而无需自己实现递归:
There are at least two options available nowadays.
Before deleting the folder, delete all its files and folders (and this means recursion!). Here is an example:
And if you are using 5.2+ you can use a RecursiveIterator to do it without implementing the recursion yourself:
我通常用它来删除文件夹中的所有文件:
然后你可以这样做
I generally use this to delete all files in a folder:
And then you can do
完成这项工作的简短函数:
我在 Utils 类中使用它,如下所示:
能力越大,责任越大:当您使用空值调用此函数时,它将删除从 root 开始的文件(<代码>/)。作为保护措施,您可以检查路径是否为空:
Short function that does the job:
I use it in a Utils class like this:
With great power comes great responsibility: When you call this function with an empty value, it will delete files starting in root (
/
). As a safeguard you can check if path is empty:正如 PHP 手册页上有关
rmdir()
的投票最多的评论中所见(请参阅 http://php.net/manual/es/function.rmdir.php),glob()
函数不返回隐藏文件。scandir()
是作为解决该问题的替代方案提供的。那里描述的算法(在我的例子中就像一个魅力)是:
As seen in most voted comment on PHP manual page about
rmdir()
(see http://php.net/manual/es/function.rmdir.php),glob()
function does not return hidden files.scandir()
is provided as an alternative that solves that issue.Algorithm described there (which worked like a charm in my case) is:
您可以使用 Symfony 的 文件系统 (code):
但是我无法用这种方法删除一些复杂的目录结构,所以首先您应该尝试一下以确保它正常工作。
我可以使用 Windows 特定实现删除上述目录结构:
为了完整起见,这是我的旧代码:
You may use Symfony's Filesystem (code):
However I couldn't delete some complex directory structures with this method, so first you should try it to ensure it's working properly.
I could delete the said directory structure using a Windows specific implementation:
And just for the sake of completeness, here is an old code of mine:
这是一个较短的版本,非常适合我
This is a shorter Version works great to me
您可以尝试如下:
You can try as follows:
我不敢相信这个问题有 30 多个答案。在 PHP 中递归删除文件夹可能需要几分钟的时间,具体取决于目录的深度和其中的文件数量!您可以使用一行代码来完成此操作...
shell_exec("rm -rf " . $dir);
如果您担心删除整个文件系统,请确保您的
$dir
路径正是您首先想要的。切勿允许用户输入可以直接删除文件的内容,而无需首先严格验证输入。这是重要的编码实践。I can't believe there are 30+ answers for this. Recursively deleting a folder in PHP could take minutes depending on the depth of the directory and the number of files in it! You can do this with one line of code ...
shell_exec("rm -rf " . $dir);
If you're concerned with deleting the entire filesystem, make sure your
$dir
path is exactly what you want first. NEVER allow a user to input something that can directly delete files without first heavily validating the input. That's essential coding practice.这对我有用:
This one works for me:
在这里,您有一个漂亮而简单的递归,用于删除源目录中的所有文件(包括该目录):
函数基于用于复制目录的递归。您可以在这里找到该功能:
使用 php 将目录的全部内容复制到另一个目录
Here you have one nice and simple recursion for deleting all files in source directory including that directory:
Function is based on recursion made for copying directory. You can find that function here:
Copy entire contents of a directory to another using php
对我来说最好的解决方案
代码:
ps记住!
不要将空值传递给任何目录删除功能!!! (始终备份它们,否则有一天您可能会遇到灾难!)
The Best Solution for me
code:
p.s. REMEMBER!
dont pass EMPTY VALUES to any Directory deleting functions!!! (backup them always, otherwise one day you might get DISASTER!!)
对 alcuadrado 的代码进行了一点修改 -
glob
看不到来自.htaccess
等点的名称的文件,所以我使用 scandir 并且脚本删除自身 - 检查__FILE__.
Litle bit modify of alcuadrado's code -
glob
don't see files with name from points like.htaccess
so I use scandir and script deletes itself - check__FILE__
.Glob 函数不会返回隐藏文件,因此当尝试递归删除树时, scandir 可能更有用。
Glob function doesn't return the hidden files, therefore scandir can be more useful, when trying to delete recursively a tree.
Linux 服务器示例:
exec('rm -f -r ' . $cache_folder . '/*');
Example for the Linux server:
exec('rm -f -r ' . $cache_folder . '/*');
这个怎么样:
What about this:
我想通过@Vijit 的评论来扩展@alcuadrado 的答案,以处理符号链接。首先,使用 getRealPath()。但是,如果您有任何文件夹符号链接,它将失败,因为它将尝试在链接上调用 rmdir - 因此您需要额外检查。
I want to expand on the answer by @alcuadrado with the comment by @Vijit for handling symlinks. Firstly, use getRealPath(). But then, if you have any symlinks that are folders it will fail as it will try and call rmdir on a link - so you need an extra check.
我更喜欢这个,因为它在成功时仍然返回 TRUE,在失败时返回 FALSE,并且它还可以防止空路径可能尝试删除 '/*' 中的所有内容的错误!!:
I prefer this because it still returns TRUE when it succeeds and FALSE when it fails, and it also prevents a bug where an empty path might try and delete everything from '/*' !!:
使用 DirectoryIterator 相当于之前的答案......
Using DirectoryIterator an equivalent of a previous answer…
您可以尝试这个简单的 12 行代码来删除文件夹或文件夹文件...
快乐编码...;) :)
you can try this simple 12 line of code for delete folder or folder files...
happy coding... ;) :)
像这样的东西吗?
Something like this?
删除文件夹中的所有文件
array_map('unlink', glob("$directory/*.*"));
删除文件夹中的所有 .*-文件(不含:“.”和“..”)
array_map('unlink', array_diff(glob("$directory/.*),array("$directory/.","$directory/..")))
现在删除文件夹本身
rmdir($目录)
Delete all files in Folder
array_map('unlink', glob("$directory/*.*"));
Delete all .*-Files in Folder (without: "." and "..")
array_map('unlink', array_diff(glob("$directory/.*),array("$directory/.","$directory/..")))
Now delete the Folder itself
rmdir($directory)
2 美分添加到上面的这个答案,顺便说一句,
在你的 glob (或类似)函数扫描/读取目录中,添加一个条件来检查响应不为空,否则将抛出
为 foreach() 提供的参数无效
警告。所以...我的完整功能(作为对象方法):
2 cents to add to THIS answer above, which is great BTW
After your glob (or similar) function has scanned/read the directory, add a conditional to check the response is not empty, or an
invalid argument supplied for foreach()
warning will be thrown. So...My full function (as an object method):
这是完美的解决方案:
Here is the solution that works perfect:
这又如何呢?
参考资料:
https://paulund.co.uk/php-delete-目录和目录中的文件
What about this?
Refrence:
https://paulund.co.uk/php-delete-directory-and-files-in-directory
您可以复制 YII 助手
$directory (string) - 以递归方式删除。
$options (array) - 用于删除目录。
有效选项有:
traverseSymlinks:布尔值,是否也应该遍历目录的符号链接。默认为
false
,表示符号链接目录的内容不会被删除。在默认情况下,只会删除符号链接。You could copy the YII helpers
$directory (string) - to be deleted recursively.
$options (array) - for the directory removal.
Valid options are:
traverseSymlinks: boolean, whether symlinks to the directories should be traversed too. Defaults to
false
, meaning that the content of the symlinked directory would not be deleted. Only symlink would be removed in that default case.您是否尝试过 php.net 上的上述代码
对我来说很好用
Have your tryed out the obove code from php.net
Work for me fine
对于窗户:
For windows:
类似于 Playnox 的解决方案,但具有优雅的内置 DirectoryIterator:
Like Playnox's solution, but with the elegant built-in DirectoryIterator:
我不记得从哪里复制了这个函数,但看起来它没有列出并且它对我有用
I do not remember from where I copied this function, but it looks like it is not listed and it is working for me