用PHP5的DirectoryIterators递归扫描目录
PHP5中增加了Iterator,一组有助于导航和处理等级数据结构的现成接口,这是PHP5最有趣的新特性之一。 这些Iterator显著减少了处理XML文档树或文件集合所需的代码。PHP5中使用大量Iterator,包括ArrayIterator、 CachingIterator、LimitIterator、RecursiveIterator、SimpleXMLIterator和 DirectoryIterator。 通过DirectoryIterator可以迅速有效地对目录中的文件进行处理。在编码过程中稍微增加一些创造力,DirectoryIterator还可用于递归处理嵌套式目录树。这两个任务只需使用几行代码就可以完成,比“标准”处理方法有了显著提高。 处理单级目录 列表A isDot()) {echo $file . "n";}}?>在浏览器中查看这段代码的输出结果,你会在指定目录中看到一个文件列表。这一切是如何发生的呢?DirectoryIterator提供一个预先确 定的接口来重述一个目录的内容;示例目标目录的位置后,就可以把它当作一个标准的PHP数组来处理,每个元素代表目录中的一个文件。注意它使用isDot ()方法分别过滤掉“.”和“..”目录。 处理嵌套式目录树 但是,使用PHP5,你只需要两个新的Iterator:RecursiveIterator和RecursiveIteratorIterator,它们组合了所有上述功能。见列表B: 列表B 这时,输入结果将列出起始目录下的所有文件和目录。不必说,如果需要处理某个特定目录级下的所有文件——例如,递归压缩一个目录树;或修改一系列嵌套文件的组/所有者许可时——使用这种递归内置接口就非常方便。 现实应用:打印一个目录树 列表C说明了DirectoryTreeIterator的用法。 列表C 以下是你看到的一部分输出结果: |-ch01| |-recipe01| | |-example01.php| | -example02.php| |-recipe02| | |-example01.php| | -example02.php| |-recipe03| | -example01.php...为更好了解这些DirectoryIterator的价值,尝试用标准的文件和目录函数对本教程中说明的三个应用编码。
首先我们从简单的任务着手:处理一个单级目录。输入(或复制)以下代码(列表A),修改目录路径以反映当地配置:
递归处理一个嵌套式目录树几乎同样简单。在这种情况下,DirectoryIterator需要检查它在单级目录中遇到的每一个对象,确定其是一个文件还是目录。如果是一个目录,就更深入一级检验下一级的内容。这听起来似乎相当复杂,在过去一般都需要15行以上的代码。
打印图形目录树是目录递归的一个常见应用。利用Iterator处理这个任务十分简单,因为Iterator类文档中包含一个专门为这个应用而编写 的实例类。DirectoryTreeIterator(感谢Marcus Boerger)为前面讨论的RecursiveIteratorIterator提供了其它改进,特别是在树结构中代表深度和位置的ASCII标记。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











The recursion depth of C++ functions is limited, and exceeding this limit will result in a stack overflow error. The limit value varies between systems and compilers, but is usually between 1,000 and 10,000. Solutions include: 1. Tail recursion optimization; 2. Tail call; 3. Iterative implementation.

Yes, C++ Lambda expressions can support recursion by using std::function: Use std::function to capture a reference to a Lambda expression. With a captured reference, a Lambda expression can call itself recursively.

The recursive algorithm solves structured problems through function self-calling. The advantage is that it is simple and easy to understand, but the disadvantage is that it is less efficient and may cause stack overflow. The non-recursive algorithm avoids recursion by explicitly managing the stack data structure. The advantage is that it is more efficient and avoids the stack. Overflow, the disadvantage is that the code may be more complex. The choice of recursive or non-recursive depends on the problem and the specific constraints of the implementation.

1. Open NetEase Cloud Music, click My, then click Local Music. 2. Click the three dots in the upper right corner. 3. Click Scan local music. 4. Click Scan Settings below. 5. Swipe left to filter audio files shorter than 60 seconds. 6. Go back and click Full Scan to scan all local music.

Tail recursion optimization (TRO) improves the efficiency of certain recursive calls. It converts tail-recursive calls into jump instructions and saves the context state in registers instead of on the stack, thereby eliminating extra calls and return operations to the stack and improving algorithm efficiency. Using TRO, we can optimize tail recursive functions (such as factorial calculations). By replacing the tail recursive call with a goto statement, the compiler will convert the goto jump into TRO and optimize the execution of the recursive algorithm.

The mobile version of WeChat Reading App is a very good reading software. This software provides a lot of books. You can read them anytime, anywhere with just one click to search and read them online. All of them are officially authorized and different types of books are neatly arranged. Sort and enjoy a comfortable and relaxing reading atmosphere. Switch the reading modes of different scenarios, update the latest book chapters continuously every day, support online login from multiple devices, and batch download to the bookshelf. You can read it with or without the Internet, so that everyone can discover more knowledge from it. Now the editor details it online Promote the method of viewing the catalog for WeChat reading partners. 1. Open the book you want to view the catalog and click in the middle of the book. 2. Click the three lines icon in the lower left corner. 3. In the pop-up window, view the book catalog

A recursive function is a technique that calls itself repeatedly to solve a problem in string processing. It requires a termination condition to prevent infinite recursion. Recursion is widely used in operations such as string reversal and palindrome checking.

In Quark software, a variety of functions bring convenience and fun to users, among which the scanning function is particularly popular. Through the scanning function, users can easily scan the QR code, whether it is to quickly log in to the website, add friends, or download applications, all can be done with one click. So, how to use the scanning function of Quark QR code? Players who still don’t know how to use it must not miss it. Come and follow the article brought by the editor of this website to learn more about it. How to scan the Quark QR code and answer: [Quark]-[Three horizontal icons]-[Take photo and scan]. Specific steps: 1. First open the Quark software. After entering the homepage, we click the [three horizontal icons] in the lower right corner; 2. Then slide up on the My Page to the bottom of the page to find [Photo Scan]
