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 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: 1402624679
Program End time: 1402624682
The time difference is very big.
Reply to discussion (solution)
Traverse the entire Site directory for 3 seconds, fast enough.
The efficiency of the traversal is just that.
To get the directory tree to work without Linux commands, look.