@@ -46,26 +46,30 @@ public static function mkdirDeep(string $dir, int $mode = 0766): bool {
46
46
* @return array
47
47
*/
48
48
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 = [];
58
52
59
- if ($ type != ' file ' ) {
60
- array_push ( $ tree , $ single ) ;
61
- }
53
+ if (! in_array ( $ type, $ typeArr ) ) {
54
+ $ type = ' all ' ;
55
+ }
62
56
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 );
65
69
}
66
- } elseif ($ type != 'dir ' ) {
67
- array_push ($ tree , $ single );
68
70
}
71
+ } elseif (is_file ($ path ) && $ type != 'dir ' ) {
72
+ array_push ($ tree , $ path );
69
73
}
70
74
71
75
return $ tree ;
@@ -215,7 +219,7 @@ public static function clearDir(string $path): bool {
215
219
@rmdir ($ dir );
216
220
}
217
221
218
- unset($ objects , $ object , $ dirs );
222
+ unset($ dir , $ iterator , $ dirs );
219
223
return true ;
220
224
}
221
225
0 commit comments