Re: Newbie: Simple site search >> To bit more Complex...

From: Date: Mon, 30 Jul 2001 21:47:54 +0000
Subject: Re: Newbie: Simple site search >> To bit more Complex...
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-60282@lists.php.net to get a copy of this message
I wrote a search script recently, and had the user specify which directories they wanted searched. You can view that here (Requires free account). I thought this a better choice as it was easier and more customizable. http://www.weberdev.com/get_example.php3?count=3014 As for searching EVERYTHING (which may cause security issues I might add) without rewriting your script too much, I would suggest you use a function to get the subdirectories and then check them with your grep statement. I threw this together for the task of getting your subdirectories function Get_Tree($directory) { global $dirs; if (substr($directory,-1) != "/") $directory = $directory . "/"; $fh = opendir($directory); while ($file = readdir($fh)) { if (is_dir($directory . $file) && $file != ".." && $file != ".") { $dirs[count($dirs)] = $directory . $file; Get_Tree($directory.$file); } } closedir($fh); } I played with static variables, as the manual says they work with recursive functions, but didn't have any luck so I just used a global variable. So you also need: $dirs = array(); Get_Tree($directory); $dirs will then be an array filled with all of the subdirectories of the directory you handed it (Probably something like $directory = dirname($SCRIPT_FILENAME); ) You should be able to then do your grep statement on the $dirs array in a simple for loop. Let me know if you have any problems with any of this =) Sheridan Saint-Michel Website Administrator FoxJet, an ITW Company www.foxjet.com ----- Original Message ----- From: Steve Wright <steve@stevewrightonline.co.uk> To: <php-general@lists.php.net> Sent: Thursday, August 30, 2001 2:07 PM Subject: [PHP] Newbie: Simple site search >> To bit more Complex... > Hey, > > I have just developed a simple site search.. .and am after learning how to > get it to search all directories... at present, it only searches the one it > is in!! > > > Here's the code: > > <? > include("include/common.inc"); > $title = "Search"; > include("include/header.inc"); > ?> > <P> > <FORM ACTION="<? echo "$PHP_SELF"; ?>" > METHOD="POST"> > <INPUT TYPE="text" NAME="searchstr" value="<? echo > "$searchstr"; ?>" > SIZE="20" MAXLENGTH="30"> > <INPUT TYPE="submit" VALUE="Search!"> > </FORM> > </P> > <? > if ( ! empty($searchstr) ) { > // empty() is used to check if we've any search string > // if we do, call grep and display the results. > echo "<HR>\n"; > // call grep with case-insensitive search mode on all files > $cmdstr = "grep -i $searchstr *"; > $fp = popen( $cmdstr, "r" ); file://open the output of command > as a pipe > $myresult = array(); // to hold my search results > while( $buffer = fgetss ($fp, 4096 )) { > // grep returns in the format > // filename: line > // So, we use split() to split the data > list($fname, $fline) = split(":",$buffer, 2); > // we take only the first hit per file > if ( !defined($myresult[$fname])) > $myresult[$fname] = $fline; > } > // we have results in a hash. lets walk through it and print > it > if ( count($myresult) ){ > echo "<OL>\n"; > while(list($fname,$fline) = each($myresult)) > echo "<LI> > <A HREF=\"$fname\">$fname</A> : $fline > </LI>\n"; > echo "</OL>\n"; > } else { > // no hits > echo "Sorry. Search on <B>$searchstr</B> > returned no results.<BR>\n"; > } > pclose($fp); > } > ?> > <? > include("include/footer.inc"); > ?> > > > > I think it centers around the line whcich contains but am not definate: > while( $buffer = fgetss ($fp, 4096 )) { > > > Any help much appreciated.. > > Steve Wright > > > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net

« previous php.general (#60282) next »