I am trying to use the PHP recursive directory iterator to find php and html files in order to list those by date. The code used is as follows:
$filelist = array();
$iterator = new RecursiveDirectoryIterator( $this->_jrootpath );
foreach(new RecursiveIteratorIterator($iterator) as $file) {
if ( !$file->isDir() ) {
if ( preg_match( "/.(html|htm|php)$/", $file->getFilename() ) ) {
$filelist[$file->getPathname()] = $file->getMTime();
}
}
}
arsort($filelist);
foreach ($filelist as $key => $val) {
$resultoutput .= ' <tr>
<td class="filepath">'.$key.'</td>
<td class="filedate">'.date("d-m-Y H:i:s",$val).'</td>
</tr>';
}
The above works, but I need to restrict the iteration to certain folders. This script will start in a parent folder where I expect some particular subfolders like 'administrator', 'components', 'modules', 'plugins' and 'templates' (in other words the standard folders of a Joomla installation). I also expect a few files in the parent folder, like 'index.php'. These files and folders should be iterated (frankly, writing this I am not sure if the parent folder itself is being iterated by my code!). However, it is possible that the parent folder also contains other subfolders which should not be iterated. These could be subfolders for subdomains or addon domains, named for example 'mysub' and 'myaddon.com'. There are two reasons why I want to skip any folder except for the normal Joomla installation folders. One is that I don't need to know about files in other folders. Moreover, iterating all folders can take so much time and resources that I get 500 error pages.
My question is how I would best change my code in order to make sure that the right folders are excluded from and included in the iteration? I have seen iterator examples around the web checking the name of a directory, but that does not help me enough, because I only need to check the second level folder names (meaning only the highest level subfolder in a path like parent/secondlevel/noneedtocheck/file.php).