title: How to do only selected Folders with a PHP Iterator?
body: <p>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:</p>
  
  <pre><code>        $filelist = array();
      $iterator = new RecursiveDirectoryIterator( $this-&gt;_jrootpath );
      foreach(new RecursiveIteratorIterator($iterator) as $file) {
          if ( !$file-&gt;isDir() ) {
              if ( preg_match( "/.(html|htm|php)$/", $file-&gt;getFilename() ) ) {
                  $filelist[$file-&gt;getPathname()] = $file-&gt;getMTime();
              }
          }
      }
      arsort($filelist);
      foreach ($filelist as $key =&gt; $val) {
          $resultoutput .= '          &lt;tr&gt;
                                          &lt;td class="filepath"&gt;'.$key.'&lt;/td&gt;
                                          &lt;td class="filedate"&gt;'.date("d-m-Y H:i:s",$val).'&lt;/td&gt;
                                      &lt;/tr&gt;';
      }
  </code></pre>
  
  <p>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.</p>
  
  <p>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).</p>