The function we want to use has scandir, which is to list the files and directories in the specified path, just like Dir.
> A more powerful Glob () function that returns the file name or directory that matches the specified pattern in the form of an array.
> Friendship remind, do not like the small evil so in front of the computer for too long, otherwise it will be like small evil as the hell of High blood sugar.
I. Traversing a single-layer folder:
The problem with > scanning a single-layer folder is that the results of two functions are different, but the performance is not significant.
The > Scandir function provides two additional lines, "." and "...", while Glob is not.
Copy Code code as follows:
function Get_dir_scandir () {
$tree = Array ();
foreach (Scandir ('./') as $single) {
echo $single. " <br/>\r\n ";
}
}
Get_dir_scandir ();
function Get_dir_glob () {
$tree = Array ();
foreach (Glob ('./* ') as $single) {
echo $single. " <br/>\r\n ";
}
}
Get_dir_glob ();
Two. Recursively traverse the file tree:
> In the question of recursive Scan folder tree, or the performance of the Glob function is better, very accurate.
The > Scandir function can be inexplicably scanned two times. /Department of the file, that is, if the minor evil has two files.
>.. /b.php and ... /a.php, the results will appear on the scan report two times, is very strange.
Copy Code code 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; Detecting directory Validity
$allfiles = Scandir ($path); Get all files and folders under directory
foreach ($allfiles as $filename) {//Traverse a directory of files and folders
if (In_array ($filename, Array ('. ', ' ... ')) continue; Ignore. And..
$fullname = $path. ' /'. $filename; Get full file path
if (Is_dir ($fullname)) {//is a directory, continue to recursion
$result [$filename] = Get_filetree_scandir ($fullname); Recursive start
}
else {
$temp [] = $filename; If it's a file, it's stored in an array
}
}
foreach ($temp as $tmp) {//storing the contents of a temporary array in an array of results
$result [] = $tmp; This allows the folder to be in front of the file in the back
}
return $result;
}
Print_r (Get_filetree_scandir ($path));
> Glob function scanning is often accurate, and will automatically follow the letter in order, looks like the best solution.
Copy Code code as follows:
$path = '.. ';
function Get_filetree ($path) {
$tree = Array ();
foreach (Glob ($path. ' /* ') as $single) {
if (Is_dir ($single)) {
$tree = Array_merge ($tree, Get_filetree ($single));
}
else{
$tree [] = $single;
}
}
return $tree;
}
Print_r (Get_filetree ($path));