(utility) PHP does not recursively traverse the directory of all the files in the code

Source: Internet
Author: User

<?PHP/** * PHP non-recursive implementation query all files in this directory * @param unknown $dir * @return multitype:|multitype:string*/functionScanfiles ($dir) { if(!Is_dir($dir )) return Array (); //compatible with each operating system $dir=RTrim(Str_replace( ‘\\‘, ‘/‘,$dir), ‘/‘ ) . ‘/‘; //stack, the default value is the incoming directory $dirs=Array($dir ); //container for placing all files $rt=Array ();  Do { //Pop Stacks $dir=Array_pop($dirs ); //Scan this directory $tmp=Scandir($dir ); foreach($tmp  as $f ) {  //Filter ...  if($f= = '. ' | |$f= = ' ... ')  Continue; //combining the current absolute path  $path=$dir.$f; //if it is a directory, press stack.   if(Is_dir($path )) {  Array_push($dirs,$path. ‘/‘ ); } Else if(Is_file($path)) {//if it's a file, put it in the container  $rt[] =$path; } }   }  while($dirs);//until there is no directory in the stack   return $rt;}?>

Attach another article: Do not recursively traverse the file under the directory

If you want to traverse all files (including subdirectories) in a directory, the first idea is to use recursion: Process the current directory first, and then process the subdirectories under the current directory. Can you do it without recursion? Before learning the data structure to see, recursion is actually the use of the stack to achieve, recursive feature is the constant call itself, the last call is the first to execute, the penultimate call is the second execution, and so on, the original call is the last execution. If we understand the principle of recursion, we can turn all the implementations of recursion into non-recursive implementations.

To traverse all files in a directory in a non-recursive way, the idea is divided into three main steps:

1. Create an array to put in the directory that will be traversed; (in fact, create a stack)

2. Loop through this array, the condition of the loop end is that the array is empty;

3. Each loop, processing an element in the array, and delete the element, if the element is a directory, then the directory of all the child elements are added to the array;

The code written in this way is as follows:

<?PHP/** * traverse all files in a directory * @param string $dir*/functionScanAll ($dir){  $list=Array(); $list[] =$dir;  while(Count($list) > 0)  {    //pops up the last element of an array    $file=Array_pop($list); //working with the current file    Echo $file." \ r \ n "; //If the directory    if(Is_dir($file))    {      $children=Scandir($file); foreach($children  as $child)      {        if($child!== '. ' &&$child!== ' ... ')        {          $list[] =$file.‘ /‘.$child; }      }    }  }}?>

I do not think there are many disadvantages of recursion, in fact, in many cases, the return design is very concise and readable, as for the efficiency problem, unless the recursion depth is particularly large, it will have an impact.

The following is a recursive implementation, as a comparison:

<?PHP/** * traverse all files in a directory (recursive implementation) * @param string $dir*/functionScanAll2 ($dir){  Echo $dir." \ r \ n "; if(Is_dir($dir))  {    $children=Scandir($dir); foreach($children  as $child)    {      if($child!== '. ' &&$child!== ' ... ') {scanAll2 ($dir.‘ /‘.$child); }    }  }}?>

Running found that the results of the two functions are slightly different, mainly in the order of printing. The order of the function's results is reversed because the order of the stacks is exactly the opposite of the order of the Scandir, and the 21st line can be changed:

$children Array_reverse (scandir($file));

(utility) PHP does not recursively traverse the directory of all the files in the code

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.