Question

Locked

Searching file names in a directory and all sub-directories.

By jvnane ·
I am trying to write a class that is responsible for searching a directory and all its sub-directories for files containing a search string in the file name. The class I have created seems to be extremely close to working, I am just having one big problem that I can't seem how to even begin troubleshooting. When the class and it's main methods run I can see that the output indicates that it is working, but I get an error message saying "The connection was reset." This message was delivered by FireFox... If i press the stop button in time, that is when i can see certain outputs that I want to check for troubleshooting. This is how I can tell that it is almost working properly. I am pretty new to php and server control also so any help, explanations, tips, etc. would be great. Here is my class. Maybe I'm making this much more complicated than it needs to be.

<?php
//This class is used to search for a string inside file names inside of a directory, $original_path, and all subdirectories
class Search
{
var $checked_files = array(); //array to hold file names of files already tested
var $files_array = array(); //array to hold file paths that contain matched files
var $dir; //dir handle
var $original_path; //directory to not be searched above... this way the entire computer isn't searched

///////////////////////////////////
//This is the first method to be called. It serves as a way to store the directory to not search above without being changed.
//INPUTS: $file_search is just the search query... only use is to pass to main search method
// $path is the original directory
///////////////////////////////////
function start($file_search,$path)
{
$this->original_path = $path;
$this->newSearch($file_search,$path);
return $this->files_array;
}

///////////////////////////////////
//This is one of the search methods. This is only used when a new directory is being serached from. This way a new dir handle
//is not created every time a new file needs to be read.
//INPUTS $path string holding current directory for file handling etc...
///////////////////////////////////
function newSearch($file_search,$path)
{
$this->dir = opendir($path);
$this->sameSearch($file_search,$path);
}

///////////////////////////////////
//This is the main search method which contains most of the actual work.
//INPUTS Same as newSearch()
//NOTE All the random echos in here were just to help me troubleshoot it.
///////////////////////////////////
function sameSearch($file_search,$path)
{
$file = readdir($this->dir);
echo "!!!$file</br>"; //this echo is just to see the filename and see if it is working

//check to see if the read file is just a . or a .. then skip it if it is
if($this->isItDot($file) == 1)
{
echo "A</br>";
$this->sameSearch($file_search,$path);
}

//This checks to see if the file has already been tested... This way a directory isn't continuously opened and searched
$was_searched = $this->wasItSearched($file);
if($was_searched != 1)
{
echo "B</br>";
$this->checked_files[count($this->checked_files)] = $file; //store the file name so it isn't used again later
$it_is_dir = $this->isItDir($file,$path); //check to see if it is a directory or a file
if($it_is_dir == 0) //not directory, check to see if it contains search query
{
echo "C</br>";
$this->doesItMatch($file,$file_search,$path);
}
elseif($it_is_dir == 1) //is directory, open that directory to search in
{
echo "D</br>";
$path = $this->changeDir($file,$path);
closedir($this->dir);
$this->newSearch($file_search,$path); //must be new to update dir handle
}
}

//check to see if we're at the end of the current directory
$end = $this->endOfDir($file);
if($end == 1)
{
echo "E</br>";
$path = $this->moveDirBack($path);
if(strcasecmp($path,"KILL") != 0) //This kill string might be unconventional, but I am kind of confused as to how
{ //booleans are treated in php... sometimes asking to check if false would produce problems
echo "F</br>"; //This statement checks if we are at the end of the directory... if so move back a directory
closedir($this->dir); //unless we are at $original_path
$this->newSearch($file_search,$path);
}
}
if($end == 0) //done testing current file so this statement starts the method over again reading from the same dir but reads a new file
{
echo "H</br>";
$this->sameSearch($file_search,$path);
}
}

///////////////////////////////////
//Checks to see if the current file being tested contains the substring $file_search, which is what the user searched for
//INPUTS Variables names never change meaning... so they are basically same as in other methods
///////////////////////////////////
function doesItMatch($file,$file_search,$path)
{
$found = stristr($file,$file_search);
if($found != false)
{
$this->storeMatch($file,$path);
}
}

///////////////////////////////////
//Returns a string with a directory path, used when the current file being tested is a directory
///////////////////////////////////
function changeDir($file,$path)
{
$path = $path."/".$file;
return $path;
}

///////////////////////////////////
//Tests to see if we are at the end of the current directory
///////////////////////////////////
function endOfDir($file)
{
if((bool)$file == FALSE) //Is this right?
{
return 1;
}
else {return 0;}
}

///////////////////////////////////
//Checks to see if the current file being tested is a . or a ..
///////////////////////////////////
function isItDot($file)
{
if(strcasecmp($file,".") == 0 || strcasecmp($file,"..") == 0)
{return 1;}
else {return false;}
}

///////////////////////////////////
//Tests to see if the current file being tested is in fact a file or just a folder
///////////////////////////////////
function isItDir($file,$path)
{
if((filetype($path."/".$file) == dir) && $file != NULL)
{
return 1;
}
else {return 0;}
}

///////////////////////////////////
//Change current directory to previous directory, used when at the end of the current directory to move back and continue searching
///////////////////////////////////
function moveDirBack($path)
{
if(strcasecmp($path,$this->original_path) == 0)
{
$path = "KILL";
return $path;
}
else
{
$pos = strripos($path,"/");
$path = substr($path,0,$pos);
return $path;
}
}

///////////////////////////////////
//Store the file name of a matched file in the array
///////////////////////////////////
function storeMatch($file,$path)
{
$this->files_array[count($this->files_array)] = $path."/".$file;
}

///////////////////////////////////
//Check to see if the current file was tested previously
///////////////////////////////////
function wasItSearched($file)
{
for($x = 0; $x < count($this->checked_files); $x++)
{
$a = count($this->checked_files);
if(strcasecmp($file,$this->checked_files[$x]) == 0)
{
return 1;
}
}
return 0;
}
}
?>

This conversation is currently closed to new comments.

4 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Answers

Collapse -

Check the timeout on your webserver

by Tony Hopkinson In reply to Searching file names in a ...

Sounds like your search is taking too long.

Is there some particular reason why you aren't using regular expressions ?

Collapse -

Regular Expressions

by jvnane In reply to Check the timeout on your ...

I haven't heard of regular expressions... As I said I am new to php. I didn't do too much reading up on it I just kind of went at it. Anyways, I'm reading up on them right now. How much faster can they make string handling? Could it be enough to fix my code? Also, The error comes up after about 3-4 seconds of searching... Is this really too long?

Collapse -

Oh they are faster , as long as you get the expression

by Tony Hopkinson In reply to Regular Expressions

right, your's is simple enough though.

3-4 seconds? default on apache is 300 seconds, not many people would go that low, A few echo statements in your script (now doing X) might be a good idea, and may be a test area with very few files to search to exercise the code as now me thinks my first answer was complete bowlocks.

Collapse -

Already on it

by jvnane In reply to Oh they are faster , as l ...

Yah, thanks for the advice. I've already set up a smaller folder with a few fake files and sub-folders to test. On a large scale it seemed like it was working, but now i can see it breaking.

Back to Networks Forum
4 total posts (Page 1 of 1)  

Related Discussions

Related Forums