Directoryiterators recursive scan of directories with PHP5

Source: Internet
Author: User
Tags foreach data structures new features

PHP5 adds iterator, a set of out-of-the-box interfaces that help navigate and process hierarchical data structures, one of the most interesting new features of PHP5.

These iterator significantly reduce the code required to process an XML document tree or a collection of files. PHP5 uses a large number of iterator, including Arrayiterator, Cachingiterator, Limititerator, Recursiveiterator, Simplexmliterator, and Directoryiterator.

The files in the directory can be processed quickly and efficiently by Directoryiterator. With a little more creativity in the coding process, directoryiterator can also be used to recursively process nested trees. These two tasks can be accomplished with just a few lines of code, which is significantly better than the "standard" approach.

Working with a single level directory

Let's start with a simple task: Process a single level directory. Enter (or copy) the following code (List a), and modify the directory path to reflect the local configuration:

List A

<?php
$it = new DirectoryIterator("/tmp/mystuff");
foreach($it as $file) {
if (!$it->isDot()) {
echo $file . "n";
}
}
?>

Looking at the output of this code in the browser, 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; 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 trees

Recursive processing of a nested tree is almost as simple as that. In this case, Directoryiterator needs to check each object it encounters in a single level directory, determining whether it is a file or a directory. If it's a directory, go deeper into the next level of content. This may sound rather complicated, and in the past it typically requires more than 15 lines of code.

However, with PHP5, you only need two new iterator:recursiveiterator and recursiveiteratoriterator that combine all of the above features. See List B:

List B

<?php
$it = new RecursiveDirectoryIterator("/tmp");
foreach(new RecursiveIteratorIterator($it) as $file) {
echo $file . "n";
}
?>

At this point, the input results will list all the files and directories in the starting directory. Needless to say, if you need to work with all the files at a particular directory level-for example, by recursively compressing a tree, or by modifying the group/owner license for a series of nested files-it is convenient to use this recursive built-in interface.

Real-World applications: Print a directory tree

Printing a graphics directory tree is a common application of directory recursion. Using iterator to handle this task is simple because the iterator class document contains an instance class written specifically for this application. Directorytreeiterator (Thanks to Marcus Boerger) provides other improvements for the Recursiveiteratoriterator discussed earlier, especially the ASCII markup that represents depth and location in the tree structure.

List C illustrates the use of directorytreeiterator.

List C

<?php
$it = new DirectoryTreeIterator("/tmp/cookbook/");
foreach($it as $path) {
echo $path . "n";
}
?>

Here are some of the output results 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 to encode the three applications described in this tutorial with standard file and directory functions.

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.