Remove empty subfolders with PHP(使用 PHP 删除空子文件夹)
问题描述
我正在开发一个 PHP 函数,该函数将递归删除所有不包含从给定绝对路径开始的文件的子文件夹.
I am working on a PHP function that will recursively remove all sub-folders that contain no files starting from a given absolute path.
这是目前开发的代码:
function RemoveEmptySubFolders($starting_from_path) {
// Returns true if the folder contains no files
function IsEmptyFolder($folder) {
return (count(array_diff(glob($folder.DIRECTORY_SEPARATOR."*"), Array(".", ".."))) == 0);
}
// Cycles thorugh the subfolders of $from_path and
// returns true if at least one empty folder has been removed
function DoRemoveEmptyFolders($from_path) {
if(IsEmptyFolder($from_path)) {
rmdir($from_path);
return true;
}
else {
$Dirs = glob($from_path.DIRECTORY_SEPARATOR."*", GLOB_ONLYDIR);
$ret = false;
foreach($Dirs as $path) {
$res = DoRemoveEmptyFolders($path);
$ret = $ret ? $ret : $res;
}
return $ret;
}
}
while (DoRemoveEmptyFolders($starting_from_path)) {}
}
根据我的测试,此功能有效,但我很高兴看到任何改进代码性能的想法.
As per my tests this function works, though I would be very delighted to see any ideas for better performing code.
推荐答案
如果您在空文件夹内有空文件夹内有空文件夹,则需要在所有文件夹中循环 3 次.所有这一切,因为您首先要进行广度测试 - 在测试其子项之前测试文件夹.相反,您应该在测试父文件夹是否为空之前进入子文件夹,这样一次就足够了.
If you have empty folder within empty folder within empty folder, you'll need to loop through ALL folders three times. All this, because you go breadth first - test folder BEFORE testing its children. Instead, you should go into child folders before testing if parent is empty, this way one pass will be sufficient.
function RemoveEmptySubFolders($path)
{
$empty=true;
foreach (glob($path.DIRECTORY_SEPARATOR."*") as $file)
{
if (is_dir($file))
{
if (!RemoveEmptySubFolders($file)) $empty=false;
}
else
{
$empty=false;
}
}
if ($empty) rmdir($path);
return $empty;
}
顺便说一下, glob 不会返回.和...条目.
By the way, glob does not return . and .. entries.
更短的版本:
function RemoveEmptySubFolders($path)
{
$empty=true;
foreach (glob($path.DIRECTORY_SEPARATOR."*") as $file)
{
$empty &= is_dir($file) && RemoveEmptySubFolders($file);
}
return $empty && rmdir($path);
}
这篇关于使用 PHP 删除空子文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 PHP 删除空子文件夹


- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- PHP - if 语句中的倒序 2021-01-01