<?php
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;
}