Hey
I have just developed a simple site search.. .and am after learning how to get it to search all directories on my web site... at present, it only searches the files of the one that 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" ); //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:
while( $buffer = fgetss ($fp, 4096 )) {