How to recursively display folders and sub folders with specific file types only using PHP5 Recursiv
- by Jared
Hi I am trying to get RecursiveDirectoryIterator class using a extension on the FilterIterator to work but for some reason it is iterating on the root directory only.
my code is this.
class fileTypeFilter extends FilterIterator
{
public function __construct($path)
{
parent::__construct(new RecursiveDirectoryIterator($path));
}
public function accept()
{
$file = $this->getInnerIterator()->current();
return preg_match('/\.php/i', $file->getFilename());
}
}
$it = new RecursiveDirectoryIterator('./');
$it = new fileTypeFilter($it);
foreach ($it as $file)
{
echo $file;
}
my directory structure is something like this.
-Dir1
--file1.php
--file2.php
-Dir2
--file1.php
etc etc
But as I said before the class is not recursively iterating over the entire directory structure and is only looking at the root.
Question is, how do use a basic RescursiveDirectoryIterator to display folders and then run the FilterIterator to only show the php files in those directorys?
Cheers