searching with a directory and subdirectories

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
sulen
Forum Commoner
Posts: 79
Joined: Wed Jul 09, 2003 4:55 pm
Location: los angeles
Contact:

searching with a directory and subdirectories

Post by sulen »

I need to create a search engine to search PHP pages with a main directory and its subdirectories for a keyword which is obtained as input from the user. It needs to return the pages in which the keyword is located.

I have written a script but for some reason its doesnt seem to work and gives me the following error

- fopen(rfp/rfpmain.php): failed to open stream: Permission denied in file name

- filesize(): Stat failed for rfp/rfpmain.php (errno=13 - Permission denied)

- fread(): supplied argument is not a valid stream resource

- fclose(): supplied argument is not a valid stream resource

- stristr(): Empty delimiter

The code is as under :

<?
$find = "$string";
$directory = "rfp";
$d = dir($directory);
while(($file = $d->read())!==false)
{
if($file!="." && $file!=".." && stristr($file, ".php")!==false)
{
$open = fopen($directory."/".$file, "rb");
$read = fread($open, filesize($directory."/".$file));
fclose($open);
clearstatcache();

if(stristr($read, $find)!==false)
{
$matches[] = $directory."/".$file;
}
}
}

echo "<b>".count($matches)."</b> matches have been found.<hr>";

if(count($matches)>0)
{
foreach($matches as $path)
{
echo "File Match : <a href=\"{$path}\">{$path}</a><br>";
}
}

?>

Can someone please help me. thanks
Farrier
Forum Newbie
Posts: 12
Joined: Thu Mar 25, 2004 10:39 am

Post by Farrier »

"Permission denied" was the first error: the others are just results of that.

Is the script file readable as user "nobody"?
Is the directory "rfp" readable and executable by user "nobody"?

You might also want to change this line:

Code: Select all

$open = fopen($directory."/".$file, "rb");
To something more like:

Code: Select all

if (($open = @fopen($directory."/".$file, "rb"))===false) {
    print("Couldn't open $file<br>\n");
    continue;
}
aleigh
Forum Commoner
Posts: 26
Joined: Thu Mar 25, 2004 11:06 am
Location: Midwestern United States
Contact:

Post by aleigh »

Try running:

<?php
echo get_current_user();
?>

It will output the user your script is executing on, and you might become enlightened if this is not what you expect.
GirishR
Forum Newbie
Posts: 11
Joined: Wed May 19, 2004 6:48 am
Contact:

Re: searching with a directory and subdirectories

Post by GirishR »

sulen wrote:I need to create a search engine to search PHP pages with a main directory and its subdirectories for a keyword which is obtained as input from the user. It needs to return the pages in which the keyword is located.
You might want to try the search engine I developed. Its called 'The Search Engine Project'. Its in http://www.hotscripts.com/Detailed/33759.html. I will be glad to help on the script.

Thanks
Girish R
Post Reply