Scan directories with PHP5 's directoryiterators recursion
Specific: http://www.verydemo.com/demo_c116_i81977.html
The addition of iterator in PHP5, a set of out-of-the-box interfaces to help navigate and manipulate hierarchical data structures, is one of PHP5 's most interesting new features.
These iterator significantly reduce the code required to process the XML document tree or file collection. A large number of iterator are used in PHP5, including Arrayiterator, Cachingiterator, Limititerator, Recursiveiterator, Simplexmliterator, and Directoryiterator.
The files in the directory can be processed quickly and efficiently through directoryiterator. Adding some creativity to the coding process, directoryiterator can also be used to recursively handle nested trees. These two tasks can be done with just a few lines of code, which is significantly better than the "standard" approach.
Working with single-level catalogs
First we start with a simple task: working with a single-level directory. Enter (or copy) the following code (list a) to modify the directory path to reflect the local configuration:
List A
$it = new DirectoryIterator("/tmp/mystuff");
foreach($it as $file) {
if (!$it->isDot()) {
echo $file . "n";
}
}
?>
View the output of this code in a browser and you will see a list of files in the specified directory. How did all this happen? Directoryiterator provides a predetermined interface to restate the contents of a directory, and after the location of the sample target directory, it can be treated as a standard PHP array, with each element representing a file in the directory. Note that it uses the Isdot () method to filter out "." Separately. and ".." Directory.
Working with nested directory trees
Recursive processing of a nested directory tree is almost as simple. In this case, Directoryiterator needs to check each object it encounters in a single-level directory to determine whether it is a file or a directory. In the case of a directory, a more in-depth examination of the next level of content. This may sound quite complicated, and in the past it would have been more than 15 lines of code.
However, using PHP5, you only need two new iterator:recursiveiterator and Recursiveiteratoriterator, which combine all of the above features. See List B:
List B
$it = new RecursiveDirectoryIterator("/tmp");
foreach(new RecursiveIteratorIterator($it) as $file) {
echo $file . "n";
}
?>
....................................................................
Reality app: Print a directory tree
Printing a graphical directory tree is a common application of directory recursion. Using iterator to handle this task is straightforward because the iterator class document contains an instance class specifically written for this application. Directorytreeiterator (Thanks to Marcus Boerger) provides other improvements for the Recursiveiteratoriterator discussed earlier, especially the ASCII markup that represents depth and position in the tree structure.
List C illustrates the use of directorytreeiterator.
List C
$it = new DirectoryTreeIterator("/tmp/cookbook/");
foreach($it as $path) {
echo $path . "n";
}
?>
Here are some of the output you see:
|-ch01
| |-recipe01
| | |-example01.php
| | -example02.php
| |-recipe02
| | |-example01.php
| | -example02.php
| |-recipe03
| | -example01.php
...
To better understand the value of these directoryiterator, try using standard file and directory functions to encode the three apps described in this tutorial.