title: How to recursively iterate through files in PHP?
body: <p>I have set up a basic script that is posting an array of paths to find template files inside them; currently it's only searching two levels deep and I'm having some troubles getting my head around the logic for an extensive loop to iterate all child directories until the length is 0.</p>
  
  <p>So if I have a structure like this:</p>
  
  <pre><code>./components
  ./components/template.html
  ./components/template2.html
  ./components/side/template.html
  ./components/side/template2.html
  ./components/side/second/template.html
  ./components/side/second/template2.html
  ./components/side/second/third/template.html
  ./components/side/second/third/template2.html
  </code></pre>
  
  <p>It's only searching up to the "side" directory for .html files when ideally I want it to check all child directories and the passed directory for .html files. Here is my working code so far:</p>
  
  <pre><code>&lt;?php
  function getFiles($path){
      $dh  = opendir($path);
      foreach(glob($path.'/*.html') as $filename){
          $files[] = $filename;
      }
      if (isset($files)) {
          return $files;
      }
  }
  foreach ($_POST as $path) {
      foreach (glob($path . '/*' , GLOB_ONLYDIR) as $secondLevel) {
          $files[] = getFiles($secondLevel);
      }
      $files[] = getFiles($path);
  }
  sort($files);
  print_r(json_encode($files));
  ?&gt;
  </code></pre>