Aaron Fischer wrote: > Greetings, > > I am working on a little script that will start at a specified directory > on my web server and then proceed to look through all files and > sub-folders for an instance of text located in a file. (This is a > follow-up of sorts to a previous post of mine from a week or two ago.) > > I got the code to work fine for searching through one directory. > However, my thought was that in order to drill down through an undefined > number of sub-folders I would need to implement a recursive function. > The recursive "depth first" search function I made is not working as > expected and so far I haven't been able to figure out what or how I need > to tweak it.
Aaron, First up, why not just use grep? Anyway, your problem is that you are not addressing the items by the full path, so the script can't find them. Here is a modified example which may help, and includes some output so you can see what is haoppening. Dan <?php $path='.'; search($path); function search($path) { echo 'Searching '. $path ."\n"; $dir=opendir($path); // browse all files and folders in the current directory while (false !== ($item=readdir($dir))) { if ($item == '.' | $item == '..') { continue; } $item = $path .'/'. $item; echo $item."\n"; // if the item is a directory, drill down using a // recursive call to the search function if (is_dir($item) && $item !='.' && $item!='..') { search($item); } if (is_file($item)) { $file=file($item); $lines=count($file); for ($i=0; $i<$lines; $i++) { if (strstr(($file[$i]), 'text to search for')) { echo 'Search string found on line ' . $i . ' in file ' . $item . '<br />'; } } } } closedir($dir); echo 'Done searching '. $path ."\n"; } // end of script _______________________________________________ New York PHP Community Talk Mailing List http://lists.nyphp.org/mailman/listinfo/talk NYPHPCon 2006 Presentations Online http://www.nyphpcon.com Show Your Participation in New York PHP http://www.nyphp.org/show_participation.php