PHP SPL标准库的用法(遍历目录,查找固定条件的文件)

[代码] [PHP]代码

01 <?php
02 class RecursiveFileFilterIterator extends FilterIterator {
03     // 满足条件的扩展名
04     protected $ext = array('jpg','gif');
05  
06     /**
07      * 提供 $path 并生成对应的目录迭代器
08      */
09     public function __construct($path) {
10         parent::__construct(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)));
11     }
12  
13     /**
14      * 检查文件扩展名是否满足条件
15      */
16     public function accept() {
17         $item = $this->getInnerIterator();
18         if ($item->isFile() &&
19                 in_array(pathinfo($item->getFilename(), PATHINFO_EXTENSION), $this->ext)) {
20             return TRUE;
21         }
22     }
23 }
24  
25 // 实例化
26 foreach (new RecursiveFileFilterIterator('D:/history') as $item) {
27     echo $item . PHP_EOL;
28 }
posted @ 2013-02-01 13:37  幻星宇  阅读(516)  评论(0编辑  收藏  举报