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

javascript - IndexedDB error: Uncaught DataCloneError: Failed to execute 'put' on 'IDBObjectStore': An object could not be cloned -

java - Unable to make sub reports with Jasper -

java - How to retrieve field values by passing field name using JPOS ISO8583 message format -