NYCPHP Meetup

[nycphp-talk] Search of directories and files on my web server

Aaron Fischer agfische at email.smith.edu
Thu Aug 16 13:36:03 EDT 2007


Dan, thanks that did the trick.  I had various echo statements in my  
original script but took them out for purposes of posting succinct  
code to the list.

I didn't use grep because I don't have any exposure or experience  
with it.  It was mentioned as an option during my original post but I  
was hesitant to try the unknown path given the duress I was under  
from time constraints.

Any suggestions for a good grep tutorial/resource on the web?  I'm  
interested in adding it to my tool belt for future use.

In going back and looking at the one or two grep examples, it seems  
that it might be a much simpler and more efficient way of solving  
this problem.

Cheers,

-Aaron


>
> 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




More information about the talk mailing list