champrock Posted September 25, 2008 Share Posted September 25, 2008 Hi I am trying to recursively search for a given file from a directory and all the subdirectories in it. I am unable to figure out how to achieve that and output all instances of the file with the complete paths. Can anyone please help me out in this? I searched php.net reference but somehow those code snippets mentioned do not return anything for me . Just a blank page. thanks a lot for all the help. Link to comment https://forums.phpfreaks.com/topic/125745-recursive-file-searching/ Share on other sites More sharing options...
GingerRobot Posted September 25, 2008 Share Posted September 25, 2008 I have this from a while ago which was used to find a file and then replace some text in it. You could modify if for your needs: <?php function find_file($dir,$file_to_find,$text_to_find,$replacement){ $handler = opendir($dir); while(false !== ($file = readdir($handler))){ if($file != '.' && $file != '..'){ if(is_dir($dir.'/'.$file)){//if this is a directory, recall the function with the subdirectory defined $sub_dir = $dir.'/'.$file; find_file($sub_dir,$file_to_find,$text_to_find,$replacement); }else{ if($file==$file_to_find){//this is the file we are looking for echo 'found'; $new_contents = str_replace($text_to_find,$replacement,file_get_contents($dir.'/'.$file)); $h = fopen($dir.'/'.$file,'w'); fwrite($h,$new_contents); fclose($h); } } } } } echo find_file($_SERVER['DOCUMENT_ROOT'],'test.txt','test','blah'); ?> Link to comment https://forums.phpfreaks.com/topic/125745-recursive-file-searching/#findComment-650283 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.