Recursive directory listing I'm trying to optimize performance for recursive directory listing with some exceptions:

function listdir($basedir) {
    global $root, $ignore_dirs, $ignore_files, $filetypes, $ignore_starts_with;
        if ($handle = @opendir($basedir)) { 
            while (false !== ($fn = readdir($handle))) { 
                if ($fn != '.' &amp;&amp; $fn != '..') {

                    $s = true;
                    foreach ($ignore_starts_with as $pre) if (strpos($fn, $pre) === 0) $s = false;

                    $dir = $basedir."/".$fn;
                    if ($s &amp;&amp; is_dir($dir) &amp;&amp; !in_array($fn, $ignore_dirs)) {
                        listdir($dir);
                    } else {

                        if ($s &amp;&amp; !in_array($fn,$ignore_files) &amp;&amp; preg_match("/[^.\/].+\.($filetypes)$/",$dir,$fname)) printlink($fname[0]);
                    } 
                } 
            } 
        closedir($handle); 
    } 
}
First of all, I'd use RecursiveDirectoryIterator, removed globals and corrected formatting.

Then used Xdebug + CacheGrind to try different versions.