How to traverse all files in a directory in php without recursion

青灯夜游
Release: 2023-03-09 22:50:02
Original
2134 people have browsed it

Implementation method: 1. Create an array and put the directory to be traversed; 2. Loop to process the array. The condition for the end of the loop is that the array is empty; 3. Each loop, process one of the arrays element, and delete the element; 4. If this element is a directory, add all sub-elements in the directory to the array, and then process the elements until the array is empty.

How to traverse all files in a directory in php without recursion

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

If you want to traverse a certain For all files in a directory (including subdirectories), the first idea that comes to mind is to use recursion: first process the current directory, and then process the subdirectories under the current directory. Is it possible without recursion? I have seen it before when I was studying data structures. Recursion is actually implemented using the stack. The characteristic of recursion is that it continuously calls itself. The last call is executed first, and the penultimate call is executed second. And so on, the initial call is the last to be executed. If you understand the principle of recursion, you can actually convert all recursive implementations into non-recursive implementations.

Use a non-recursive method to traverse all files in a directory. The idea is mainly divided into the following steps:

  • Create an array and put the directory to be traversed; (in fact, it creates a stack)

  • Loop to process this array, loop The end condition is that the array is empty;

  • # Each loop processes an element in the array and deletes the element;

  • If this element is a directory, add all the sub-elements in the directory to the array, and then process the elements until the array is empty.

The code written according to this idea is as follows:

<?php 

/**
 * 遍历某个目录下的所有文件
 * @param string $dir
 */
function scanAll($dir)
{
  $list = array();
  $list[] = $dir;
 
  while (count($list) > 0)
  {
    //弹出数组最后一个元素
    $file = array_pop($list);
 
    //处理当前文件
    echo $file."\r\n";
 
    //如果是目录
    if (is_dir($file))
    {
      $children = scandir($file);
      foreach ($children as $child)
      {
        if ($child !== &#39;.&#39; && $child !== &#39;..&#39;)
        {
          $list[] = $file.&#39;/&#39;.$child;
        }
      }
    }
  }
}

?>
Copy after login

Here I am not I don’t think recursion has any major disadvantages. In fact, in many cases, using recursion to design is very concise and readable. As for efficiency issues, it will only have an impact unless the recursion depth is particularly large.

The following is the recursive implementation for comparison:

<?php 

/**
 * 遍历某个目录下的所有文件(递归实现)
 * @param string $dir
 */
function scanAll2($dir)
{
  echo $dir."\r\n";
 
  if (is_dir($dir))
  {
    $children = scandir($dir);
    foreach ($children as $child)
    {
      if ($child !== &#39;.&#39; && $child !== &#39;..&#39;)
      {
        scanAll2($dir.&#39;/&#39;.$child);
      }
    }
  }
}

?>
Copy after login

It is found that the results of the two functions are slightly different. There are differences, mainly in the order of printing. The order of the running results of function 1 is reversed because the order of pushing onto the stack is exactly the opposite of the order in which scandir comes out. You can change line 21:

$children = array_reverse(scandir($file));
Copy after login

Recommended study: "PHP video tutorial

The above is the detailed content of How to traverse all files in a directory in php without recursion. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template