这对你应该有用:
这里我使用了
glob()
(PHP 4 >= 4.3.0, PHP 5)从特定路径获取所有目录。然后,我遍历每个目录并检查它是否为空。
如果它是空的,我使用
rmdir()
删除它,否则我检查它是否有另一个目录,并使用新目录调用函数。
<?php
function removeEmptyDirs($path, $checkUpdated = false, $report = false) {
$dirs = glob($path . "/*", GLOB_ONLYDIR);
foreach($dirs as $dir) {
$files = glob($dir . "/*");
$innerDirs = glob($dir . "/*", GLOB_ONLYDIR);
if(empty($files)) {
if(!rmdir($dir))
echo "Err: " . $dir . "<br />";
elseif($report)
echo $dir . " - removed!" . "<br />";
} elseif(!empty($innerDirs)) {
removeEmptyDirs($dir, $checkUpdated, $report);
if($checkUpdated)
removeEmptyDirs($path, $checkUpdated, $report);
}
}
}
?>
removeEmptyDirs
(PHP 4 >= 4.3.3, PHP 5)
removeEmptyDirs — Removes empty directory's
void removeEmptyDirs( string $path [, bool $checkUpdated = false [, bool $report = false ]] )
Description
The removeEmptyDirs() function goes through a directory and removes every empty directory
Parameters
path
The Path where it should remove empty directorys
checkUpdated
If it is set to TRUE it goes through each directory again if one directory got removed
report
If it is set to TRUE the function outputs which directory get's removed
Return Values
None
As an example:
If $checkUpdated
is TRUE
structures like this get's deleted entirely:
- dir
| - file.txt
| - dir
| - dir
Result:
- dir
| - file.txt
If it is FALSE
like in default the result would be:
- dir
| - file.txt
| - dir //See here this is still here
If $report
is TRUE
you get a output like this:
test/a - removed!
test/b - removed!
test/c - removed!
Else you get no output
DirectoryIterator
的解决方案,还是可以使用glob
或其他方法? - Rizier123