Home  >  Article  >  Backend Development  >  Function code for traversing folders and subdirectories using PHP_PHP tutorial

Function code for traversing folders and subdirectories using PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:23:45793browse

The function we are going to use is Scandir, which lists the files and directories in a specified path, just like Dir.

Function code for traversing folders and subdirectories using PHP_PHP tutorial
> 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));

www.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...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn