_php Tutorials for directoryiterators recursive scanning of directories with PHP5

Source: Internet
Author: User

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

Isdot ()) {echo $file. "N";}}? > View the output of this code in a 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, 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

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, to compress a directory tree recursively, or to modify a group/owner license for a series of nested files--it is convenient to use this recursive built-in interface.

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

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.

http://www.bkjia.com/PHPjc/486632.html www.bkjia.com true http://www.bkjia.com/PHPjc/486632.html techarticle 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 processing of XML document trees or ...

  • Related Article

    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.