PHP: Scanning dir for files on each load
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/13622677/caching-readdir">Caching readdir()</a>  </p>
</blockquote>



<p>I have my site set up so that I have clean URLs and the files themselves are in various levels of folders.  To manage it, I have the following script to direct my include farther down in the page.</p>

<pre><code>    function listFolderFiles($dir,$exclude){
    global $flist;
    $ffs = scandir($dir); 
    foreach($ffs as $ff)
    { 
        if(is_array($exclude) and !in_array($ff,$exclude))
        { 
            if($ff != '.' &amp;&amp; $ff != '..')
            { 
                if(!is_dir($dir.'/'.$ff))
                {       
            }
            if(is_dir($dir.'/'.$ff)) 
                {
                listFolderFiles($dir.'/'.$ff,$exclude);
            } 
            if((is_dir($dir.'/'.$ff)) != 1 &amp;&amp; strtolower(substr($ff, strrpos($ff, '.') + 1)) == 'php')
                {
                    $name = basename($ff, ".php");
                    $flist[] .= $name;                  
                }
            }
        } 
    } 
}
listFolderFiles('content/',array('filelist.php')); 
$removing = array('_en','_es');
$pages = str_replace($removing, "", $flist);
unset($dir, $exclude, $flist);
listFolderFiles('content/Beginner/',array('filelist.php')); 
$removing = array('_en','_es');
$beginner = str_replace($removing, "", $flist);
unset($dir, $exclude, $flist);
listFolderFiles('content/Intermediate/',array('filelist.php', $beginner));
$removingi = array('_en','_es');
$intermediate = str_replace($removingi, "", $flist);
unset($dir, $exclude, $flist);
listFolderFiles('content/Advanced/',array('filelist.php', $beginner));
$removinga = array('_en','_es');
$advanced = str_replace($removinga, "", $flist);
</code></pre>

<p>Is there a faster way to execute this code, as it is currently used on every pageload, and I only need to update it when I add a new file.</p>
