If you want to traverse all the files (including subdirectories) in a directory, the first thought is to use recursion: first process the current directory, and then process the subdirectories under the current directory. Can you do without recursion? Before the data structure of the time to see, recursion is actually the use of the stack to achieve, the characteristics of recursion is constantly called itself, the last call is the first execution, the penultimate call is the second execution, and so on, and so on, the initial call is final execution. If we understand the principle of recursion, we can actually convert all the implementations of recursion into a non recursive implementation.
To iterate through all the files in a directory in a non recursive way, the main idea is three steps:
1. Create an array that will be traversed by the directory (in fact, create a stack)
2. Loop over the array, the condition that the loop ends is that the array is empty;
3. Each loop, processing an element in the array, and delete the element, if this element is a directory, then add all the child elements of the directory to the array;
The code written in this way is as follows:
/**
* Traverse all files in a directory
* @param string $dir
* FROM veryhuo.com
*/
function Scanall ($dir)
{
$list = Array ();
$list [] = $dir;
while (count ($list) > 0)
{
pops up the last element of the array
$file = Array_pop ($list);
Working with the current file
echo $file. " RN ";
If it is a directory
if (Is_dir ($file))
{
$children = Scandir ($file);
foreach ($children as $child)
{
if ($child!== '. ' && $child!== ' ... ')
{
$list [] = $file. ' /'. $child;
}
}
}
}
}
Here I do not think recursion has a lot of shortcomings, in fact, in many cases, with the return design is very concise and readable, as to the efficiency of the problem, unless the depth of the recursion is particularly large, it will have an impact.
The following is a recursive implementation, in contrast:
/**
* Traverse all files in a directory (recursive implementation)
* @param string $dir
*/
function ScanAll2 ($dir)
{
Echo $dir. " RN ";
if (Is_dir ($dir))
{
$children = Scandir ($dir);
foreach ($children as $child)
{
if ($child!== '. ' && $child!== ' ... ')
{
ScanAll2 ($dir. '/'. $child);
}
}
}
}
Run found that the results of two functions slightly different, mainly in the order of printing. The order of the results of a function is inverted, because the order of the stack is exactly the same as the order of the Scandir, you can change the 21st line:
$children = Array_reverse (Scandir ($file));