PHP Directory iterator recursivedirectoryiterator How to improve efficiency
Iterator mode
Echo ' program start: '. Time (). '
';
$path = ' d:/www ';
Getrevdir ($path);
Echo ' program end: '. Time (). '
';
Exit
Echo '
';
Get all directories and filenames under the current path
This usage is mainly used in the B/s program to obtain the remote server directory
Var_dump (Array_keys (Iterator_to_array (New Recursivedirectoryiterator ($path)));
Exit
Iterate to get all directories and files under the current path
function Getrevdir ($path, $level = 0) {
$dirIterator = new Recursivedirectoryiterator ($path);
$strSplitBar = ";
for ($i =0; $i < $level; $i + +) {
if ($i = = $level-1) {
$strSplitBar. = ' |__ ';
}else{
$strSplitBar. = ';
}
}
foreach ($dirIterator as $key = = $fileInfo) {
if ($dirIterator->haschildren ()) {
$dirName = substr ($key, Strrpos ($key, Directory_separator) +1);
Echo $strSplitBar. $dirName. '
';
Getrevdir ($key, $level + 1);
}else{
Echo $strSplitBar. basename ($key). '
';
}
}
}
Execution results
Program Start time: 1402624676
Program End time: 1402624682
Opendir Way
Echo ' program start: '. Time (). '
';
$path = ' d:/www ';
Getrevdir ($path);
Echo ' program end: '. Time (). '
';
Exit
Echo '//Get all directories and filenames under current path
//This usage is mainly used in the B/s program to obtain the remote server directory
Var_dump (Array_keys (Iterator_to_array (new Recursivedirectoryiterator ($path))));
Exit;
//iteration gets all directories and files under the current path
function Getrevdir ($path, $level = 0) {
$dirIterator = new Recursivedirectoryiterator ($path);
$strSplitBar = ';
for ($i =0; $i < $level; $i + +) {
if ($i = = $level-1) {
$strSplitBar. = ' |__ ';
} else{
$strSplitBar. = ';
}
}
foreach ($dirIterator as $key = + $fileInfo) {
if ($dirIterator->haschildren ()) {
$dirName = substr ( $key, Strrpos ($key, Directory_separator) +1);
Echo $strSplitBar. $dirName. '
';
Getrevdir ($key, $level + 1);
}else{
Echo $strSplitBar. basename ($key). '
';
}
}
}
Execute result
program start time: 1402624679
Program End time: 1402624682
Time difference is great.
------Solution--------------------
Traverse the entire Web site directory for 3 seconds, quickly
------solution--------------------
The efficiency of the traversal is the same.
to get the directory tree to be processed without Linux commands.