The function we are going to use is Scandir, which lists the files and directories in a specified path, just like Dir.
> and the more powerful Glob() function, which returns file names or directories that match the specified pattern in the form of an array.
> Friendly reminder, don’t stay in front of the computer for too long like Xiaoxie, otherwise you will suffer from high blood sugar like Xiaoxie.
1. Traverse single-layer folders:
> The problem with scanning single-layer folders is that although the results of the two functions are different, the performance is not much different.
> The Scandir function will provide two extra lines, namely "." and "..", while Glob does not.
Copy code The code is as follows:
function get_dir_scandir(){
$tree = array();
foreach(scandir('./') as $single){
echo $single."
rn";
}
}
get_dir_scandir();
function get_dir_glob(){
$tree = array();
foreach(glob('./*') as $single){
echo $single."
rn ";
}
}
get_dir_glob();
2. Recursively traverse the file tree:
> On the issue of recursively scanning the folder tree , or the Glob function performs better, to be precise.
> The Scandir function will inexplicably scan the file at ../ twice, that is to say, if Xiaoxie has two files.
> ../b.php and ../a.php, the results will appear twice on the scan report, which is very strange.
Copy code 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 directory validity
$allfiles = scandir($path); //Get 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; //Get the full file path
if (is_dir($fullname)) { //If it is a directory, continue the recursion
$result[$filename] = get_filetree_scandir( $fullname); //Recursion starts
}
else {
$temp[] = $filename; //If it is a file, store it in the array
}
}
foreach ($temp as $tmp) { //Save the contents of the temporary array into the array that saves the results
$result[] = $tmp; //This allows the folder to be sorted in the front and the file in the back
}
return $result;
}
print_r(get_filetree_scandir($path));
> The Glob function scan is very accurate and will automatically sort the order alphabetically. It seems is the best solution.
Copy code The code is 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));
http://www.bkjia.com/PHPjc/324411.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324411.htmlTechArticleThe function we want to use is Scandir, its function is to list the files and directories in the specified path, like Same as Dir. With the more powerful Glob() function, the function is to return the same as...