用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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Chinese version
Chinese version, very easy to use