Skip to content

Commit 157e816

Browse files
committed
fix 修改DirectoryHelper::getFileTree,弃用glob
1 parent 8a8a65b commit 157e816

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

src/Helpers/DirectoryHelper.php

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,26 +46,30 @@ public static function mkdirDeep(string $dir, int $mode = 0766): bool {
4646
* @return array
4747
*/
4848
public static function getFileTree(string $path, string $type = 'all', bool $recursive = true): array {
49-
$path = rtrim($path, DIRECTORY_SEPARATOR);
50-
$tree = [];
51-
// '{.,*}*' 相当于 '.*'(搜索.开头的隐藏文件)和'*'(搜索正常文件)
52-
foreach (glob($path . '/{.,*}*', (defined(GLOB_BRACE) ? GLOB_BRACE : 1024)) as $single) {
53-
if (is_dir($single)) {
54-
$file = str_replace($path . '/', '', $single);
55-
if ($file == '.' || $file == '..') {
56-
continue;
57-
}
49+
$typeArr = ['all', 'dir', 'file'];
50+
$path = rtrim($path, DIRECTORY_SEPARATOR);
51+
$tree = [];
5852

59-
if ($type != 'file') {
60-
array_push($tree, $single);
61-
}
53+
if (!in_array($type, $typeArr)) {
54+
$type = 'all';
55+
}
6256

63-
if ($recursive) {
64-
$tree = array_merge($tree, self::getFileTree($single, $type, $recursive));
57+
if (is_dir($path)) {
58+
$dir = new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS);
59+
$iterator = new RecursiveIteratorIterator($dir, RecursiveIteratorIterator::SELF_FIRST);
60+
61+
foreach ($iterator as $single => $file) {
62+
$fpath = $file->getRealPath();
63+
if ($file->isDir()) {
64+
if ($type != 'file') {
65+
array_push($tree, $fpath);
66+
}
67+
} elseif ($type != 'dir') {
68+
array_push($tree, $fpath);
6569
}
66-
} elseif ($type != 'dir') {
67-
array_push($tree, $single);
6870
}
71+
} elseif (is_file($path) && $type != 'dir') {
72+
array_push($tree, $path);
6973
}
7074

7175
return $tree;
@@ -215,7 +219,7 @@ public static function clearDir(string $path): bool {
215219
@rmdir($dir);
216220
}
217221

218-
unset($objects, $object, $dirs);
222+
unset($dir, $iterator, $dirs);
219223
return true;
220224
}
221225

0 commit comments

Comments
 (0)