在 PHP 中使用 rmdir 删除文件夹
我有一个 txt 文件,其中列出了我想要从服务器中删除的一些文件夹(用逗号分隔)。
txt 文件包含例如:
folder1,folder1/folder2,folder1/folder2/folder3
我尝试使用 rmdir 删除所有文件夹。问题是 rmdir 不会删除其中有任何文件夹的文件夹,并且 txt 文件遗憾地以错误的顺序列出了文件夹。
有什么解决办法吗? (文件夹不会包含任何文件)
这是代码:
$text_file = "folders.txt";
$all_folders_separated_by_comma = file_get_contents($text_file);
function not_empty_string($s) { return $s !== ""; }
$separate_all_folders = array_filter(explode(',', $all_folders_separated_by_comma), 'not_empty_string');
foreach ($separate_all_folders as $folder) {
rmdir($folder);
}
I have a txt file where I list some folders I want to remove from the server (separated by comma).
The txt file contains for example:
folder1,folder1/folder2,folder1/folder2/folder3
I am trying to use rmdir to remove all the folders. The problem is that rmdir wont remove the folders if there are any folders within, and the txt file sadly list the folders in the wrong order.
Any solution? (the folders will not contain any files)
Here is the code:
$text_file = "folders.txt";
$all_folders_separated_by_comma = file_get_contents($text_file);
function not_empty_string($s) { return $s !== ""; }
$separate_all_folders = array_filter(explode(',', $all_folders_separated_by_comma), 'not_empty_string');
foreach ($separate_all_folders as $folder) {
rmdir($folder);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种方法是对文件夹数组进行反向排序
rsort 来修复顺序。然后检查文件名确实是一个带有 is_dir 的文件夹。
One way would be to reverse sort the folder array
rsort to fix the ordering. Then check that the filename is indeed a folder with is_dir.