Home > Backend Development > PHP Tutorial > 解析PHP SPL标准库的用法(遍历目录,查找固定条件的文件)_php技巧

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-17 09:00:31
Original
1025 people have browsed it

 class RecursiveFileFilterIterator extends FilterIterator {
     // 满足条件的扩展名
     protected $ext = array('jpg','gif');

     /**
      * 提供 $path 并生成对应的目录迭代器
      */
     public function __construct($path) {
         parent::__construct(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)));
     }

     /**
      * 检查文件扩展名是否满足条件
      */
     public function accept() {
         $item = $this->getInnerIterator();
         if ($item->isFile() && 
                 in_array(pathinfo($item->getFilename(), PATHINFO_EXTENSION), $this->ext)) {
             return TRUE;
         }
     }
 }

 // 实例化
 foreach (new RecursiveFileFilterIterator('D:/history') as $item) {
     echo $item . PHP_EOL;
 }

Related labels:
php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template