Skip to content

Commit

Permalink
fix: 优化目录清理函数
Browse files Browse the repository at this point in the history
  • Loading branch information
zoujingli committed Oct 16, 2024
1 parent 624fb61 commit 9c1d02f
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/extend/ToolsExtend.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,15 @@ public static function copyfile(string $frdir, string $todir, array $files = [],
*/
public static function removeEmptyDirectory(string $path): bool
{
foreach (self::findFilesYield($path, null, null, true) as $item) {
($item->isFile() || $item->isLink()) ? unlink($item->getPathname()) : rmdir($item->getPathname());
}
return is_file($path) ? unlink($path) : (!is_dir($path) || rmdir($path));
$dirs = [$path];
iterator_to_array(self::findFilesYield($path, null, function (SplFileInfo $file) use (&$dirs) {
$file->isDir() ? $dirs[] = $file->getPathname() : unlink($file->getPathname());
}));
usort($dirs, function ($a, $b) {
return strlen($b) <=> strlen($a);
});
foreach ($dirs as $dir) rmdir($dir);
return !file_exists($path);
}

/**
Expand Down

0 comments on commit 9c1d02f

Please sign in to comment.