[代码] [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 }

相关文章:

  • 2022-12-23
  • 2022-02-04
  • 2022-12-23
  • 2022-12-23
  • 2021-06-13
  • 2022-12-23
  • 2022-12-23
  • 2021-08-21
猜你喜欢
  • 2021-09-13
  • 2022-12-23
  • 2022-01-20
  • 2022-12-23
  • 2021-06-04
  • 2022-12-23
相关资源
相似解决方案