PHP does not recursively traverse the code of all files in the directory _php tutorial

Source: Internet
Author: User
Implementation code:

/** * PHP non-recursive implementation query all files under this directory * @param unknown $dir * @return multitype:|multitype:string */function scanfiles ($dir) {if (! I S_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);  The container where all the files are placed $rt = Array ();  do {//Stack $dir = Array_pop ($dirs);  Scan this directory $tmp = Scandir ($dir);  foreach ($tmp as $f) {  //filter ...  if ($f = = '. ' | | $f = = ' ... ')  Continue;    Combines 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 is a file, put in the container  $rt [] = $path;}}  } while ($dirs);//until there is no directory return in the stack  $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:

/** * Traverse all files in a directory * @param string $dir */function scanAll ($dir) {  $list = array ();  $list [] = $dir;  while (count ($list) > 0)  {    //popup array last element    $file = Array_pop ($list);    Handles the current file    echo $file. \ r \ n ";    If it is a 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:

/** * Traverse all files in a directory (recursive implementation) * @param string $dir */function scanAll2 ($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));

So the result is exactly the same.

http://www.bkjia.com/PHPjc/824643.html www.bkjia.com true http://www.bkjia.com/PHPjc/824643.html techarticle Implementation code:/** * PHP non-recursive implementation query all files in that directory * @param unknown $dir * @return multitype:|multitype:string */function scanfiles ($dir) {if (! Is_dir ($dir ...

  • 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.