PHP Directory iterator recursivedirectoryiterator How to improve efficiency

Source: Internet
Author: User
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.
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.