PHP Recursively search for a file while ignoring given directories -


this question has answer here:

i'm trying create function efficiently search document root file , return path. have following recursivedirectoryiterator , regexiterator.

<?php  function find_file($filename) {     $pattern = preg_quote($filename);     $dir = new recursivedirectoryiterator($_server['document_root']);     $iterate = new recursiveiteratoriterator($dir);     $reg_iterate = new regexiterator($iterate, "/.*$pattern.*/", regexiterator::get_match);     $ignore_pattern = '(\/?)(_*?)(old|archive|backup|cache|node_modules)';     foreach($reg_iterate $matches){         foreach($matches $file){             $file = rtrim($file, '.');             if(!preg_match("/$ignore_pattern/i", $file)){                 return $file;             }         }     } }  echo find_file('styles.css');  ?> 

i'm trying though ignore directories searching, performance reasons. i'd rather not scanning through directories known fruitless. currently, can ignore patterns of paths after receiving list of files recursiveiteratoriterator, it's slow. there smarter method might recommend?

you try glob: http://php.net/manual/en/function.glob.php. easy use , it's fast.


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -