class RecursiveFileFilterIterator extends FilterIterator {
// 满足条件的扩展名
protected $ext = array('jpg','gif');
/**
* Provide $path and generate the corresponding directory iterator
*/
public function __construct($path) {
parent::__construct(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)));
}
/**
* Check whether the file extension meets the conditions
*/
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;
}