Php recursively traverses the file tree code. The Glob function performs better on recursive scanning of the folder tree. The Scandir function does not scan the files at .. twice. that is to say, if the problem of recursive scan of the folder tree is a little better than the Glob function, it is very accurate. > The Scandir function may scan files at ../twice, that is, if there are two files in Xiao Xie.
> ../B. php Tutorial and ../a. php, the results will appear twice in the scan report, which is very strange.
The code is as follows: |
|
// Update at 2010.07.25-the following code is invalid: $ Path = '..'; Function get_filetree_scandir ($ path ){ $ Tree = array (); Foreach (scandir ($ path) as $ single ){ If (is_dir ('../'. $ single )){ $ Tree = array_merge ($ tree, get_filetree ($ single )); } Else { $ Tree [] = '../'. $ single; } } Return $ tree; } Print_r (get_filetree_scandir ($ path )); // Update at 2010.07.25-The following is the new code. $ Path = './'; Function get_filetree_scandir ($ path ){ $ Result = array (); $ Temp = array (); If (! Is_dir ($ path) |! Is_readable ($ path) return null; // Check the directory validity $ Allfiles = scandir ($ path); // Obtain all files and folders in the directory Foreach ($ allfiles as $ filename) {// traverse the files and folders in the directory If (in_array ($ filename, array ('.', '..') continue; // Ignore. and .. $ Fullname = $ path. '/'. $ filename; // obtain the complete file path. If (is_dir ($ fullname) {// if it is a directory, continue recursion $ Result [$ filename] = get_filetree_scandir ($ fullname); // start recursion } Else { $ Temp [] = $ filename; // if it is a file, it is saved to an array. } } Foreach ($ temp as $ tmp) {// save the content of the temporary array to the array that saves the result $ Result [] = $ tmp; // in this way, the folder is placed in front of the file } Return $ result; } Print_r (get_filetree_scandir ($ path )); |
The Glob function performs better and is very accurate. > The Scandir function will scan the file at ../twice, that is, if it is small...