How to recursively display folders and sub folders with specific file types only using PHP5 Recursiv

Posted by Jared on Stack Overflow See other posts from Stack Overflow or by Jared
Published on 2010-03-30T00:14:28Z Indexed on 2010/03/30 3:13 UTC
Read the original article Hit count: 755

Filed under:

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

© Stack Overflow or respective owner

Related posts about php