Skip to content

Commit

Permalink
fix 修改DirectoryHelper::getFileTree,弃用glob
Browse files Browse the repository at this point in the history
  • Loading branch information
kakuilan committed Oct 15, 2020
1 parent 8a8a65b commit 157e816
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions src/Helpers/DirectoryHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,30 @@ public static function mkdirDeep(string $dir, int $mode = 0766): bool {
* @return array
*/
public static function getFileTree(string $path, string $type = 'all', bool $recursive = true): array {
$path = rtrim($path, DIRECTORY_SEPARATOR);
$tree = [];
// '{.,*}*' 相当于 '.*'(搜索.开头的隐藏文件)和'*'(搜索正常文件)
foreach (glob($path . '/{.,*}*', (defined(GLOB_BRACE) ? GLOB_BRACE : 1024)) as $single) {
if (is_dir($single)) {
$file = str_replace($path . '/', '', $single);
if ($file == '.' || $file == '..') {
continue;
}
$typeArr = ['all', 'dir', 'file'];
$path = rtrim($path, DIRECTORY_SEPARATOR);
$tree = [];

if ($type != 'file') {
array_push($tree, $single);
}
if (!in_array($type, $typeArr)) {
$type = 'all';
}

if ($recursive) {
$tree = array_merge($tree, self::getFileTree($single, $type, $recursive));
if (is_dir($path)) {
$dir = new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS);
$iterator = new RecursiveIteratorIterator($dir, RecursiveIteratorIterator::SELF_FIRST);

foreach ($iterator as $single => $file) {
$fpath = $file->getRealPath();
if ($file->isDir()) {
if ($type != 'file') {
array_push($tree, $fpath);
}
} elseif ($type != 'dir') {
array_push($tree, $fpath);
}
} elseif ($type != 'dir') {
array_push($tree, $single);
}
} elseif (is_file($path) && $type != 'dir') {
array_push($tree, $path);
}

return $tree;
Expand Down Expand Up @@ -215,7 +219,7 @@ public static function clearDir(string $path): bool {
@rmdir($dir);
}

unset($objects, $object, $dirs);
unset($dir, $iterator, $dirs);
return true;
}

Expand Down

0 comments on commit 157e816

Please sign in to comment.