PHP Recursively search for a file while ignoring given directories -
this question has answer here:
- recursive file search (php) 6 answers
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
Post a Comment