Recursively scan indexes with DirectoryIterators of PHP5

Source: Internet
Author: User
Use the DirectoryIterators of PHP5 to scan the directory recursively. Specifically, Iterator is added to www.verydemo.comdemo_c1__i81977.html PHP5. this is one of the most interesting new features of PHP5. These Iterator significantly reduces the code required to process the XML document tree or file set. PHP uses PHP5 DirectoryIterators to recursively scan directories

Specific: http://www.verydemo.com/demo_c116_i81977.html

Iterator is added to PHP5, a set of ready-made interfaces that help you navigate and process hierarchical data structures. this is one of PHP5's most interesting new features.

These Iterator significantly reduces the code required to process the XML document tree or file set. PHP5 uses a large number of Iterator, including ArrayIterator, CachingIterator, LimitIterator, RecursiveIterator, SimpleXMLIterator, and DirectoryIterator.

DirectoryIterator can be used to quickly and effectively process files in a directory. A little more creativity is added in the coding process. DirectoryIterator can also be used to recursively process nested directory trees. The two tasks can be completed with only a few lines of code, which is significantly higher than the "standard" processing method.

Process single-level directories

First, let's start with a simple task: processing a single-level Directory. Enter (or copy) the following code (List A) to modify the directory path to reflect the local configuration:

List

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

View the output result of this code in the browser. you will see a file list in the specified directory. How did this happen? DirectoryIterator provides a predetermined interface to repeat the content of a directory. after the location of the target directory in the example, it can be processed as a standard PHP array, each element represents a file in the directory. Note that the isDot () method is used to filter out the "." and "." directories respectively.

Process nested directory tree

Recursive processing of a nested directory tree is almost as simple. In this case, DirectoryIterator needs to check every object it encounters in a single-level directory to determine whether it is a file or a directory. If it is a directory, it will further examine the content of the next level. This may sound quite complicated. in the past, more than 15 lines of code were required.

However, to use PHP5, you only need two new Iterator: RecursiveIterator and RecursiveIteratorIterator, which combine all the above functions. See List B:

List B

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

........................................ ............................

Practical application: print a directory tree

Print a graphical directory tree is a common application of directory recursion. Iterator is easy to use to process this task, because the Iterator class document contains an instance class specially compiled for this application. DirectoryTreeIterator (thanks to Marcus Boerger) for providing other improvements for the RecursiveIteratorIterator discussed earlier, especially the ASCII mark that represents depth and location in the tree structure.

List C describes the usage of DirectoryTreeIterator.

List C

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

The following are some output results:

|-ch01
| |-recipe01
| | |-example01.php
| | -example02.php
| |-recipe02
| | |-example01.php
| | -example02.php
| |-recipe03
| | -example01.php
...

To better understand the value of these DirectoryIterator, try coding the three applications described in this tutorial with standard files 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.