> ../B. php Tutorial and ../a. php, the results will appear twice in the scan report, which is very strange.
| The code is as follows: |
Copy code |
|
// 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 )); |