NYCPHP Meetup

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

Edward Potter edwardpotter at gmail.com
Thu Aug 16 17:48:29 EDT 2007


Yipes,  grep -r  'foo'    *     [or something like that!]

:-)  ed

On 8/16/07, Aaron Fischer <agfische at email.smith.edu> 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.
>
> I'm completely squeezed for time at work so I'll continue to bang
> away at it but I thought I would post it here in the hopes that some
> of our rockets scientists can show me the error of my ways.  I'm
> guessing this will be a rather simple problem for some folks on the
> list.
>
> So here goes.  Listed below are my algorithm and code.  I'm looking
> forward to finding out where I'm going wrong.  =)
>
> My algorithm:
> 1) Set the directory that the script will start in.
> 2) Open that directory and read all files and folders in that directory.
> 3) While each item is being read, check it.  If the item is a
> directory, call the recursive search in order to drill down further.
> If the item is a file, open the file and search for the text string
> that I am trying to locate.
>
> My code (with comments):
>
> <?php
> $path='/home/usr/account';
> search($path);
>
> function search($path)
> {
>         $dir=opendir($path);
>         // browse all files and folders in the current directory
>         while (false !== ($item=readdir($dir))) {
>                 // if the item is a directory, drill down using a recursive call to
> the search function
>                 if (is_dir($item) && $item !='.' && $item!='..') {
>                         search($path . '/' . $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 ' . $path .
> '/' . $item . '<br />';
>                                 }
>                         }
>                 }
>         }
>         closedir($dir);
> }
> ?>
>
> Cheers,
>
> -Aaron
> _______________________________________________
> 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
>


-- 
the Blog: http://www.utopiaparkway.com
the Karma: http://www.coderswithconscience.com
the Projects: http://flickr.com/photos/86842405@N00/
the Store: http://astore.amazon.com/httpwwwutopic-20



More information about the talk mailing list