Slow search code
Posted: Fri Jul 10, 2009 1:57 am
I'm trying to make this search function on this intranet site we have at work. It's supposed to search all the file names in our databases for whatever the user puts in. The code I have created works, but it takes forever to fully execute... I haven't seen it finish when searching on the actual database, but i think it would take at least 10 minutes. Any suggestions on speeding this up would be much appreciated. Also I am pretty new to php, just started using it a few weeks ago, so I might be unfamiliar with a lot of terms etc.
<?php
class Search
{
var $original_path;
var $original_dir;
var $match_files = array();
var $dirs = array();
var $files = array();
var $file_search;
function start($file_search,$path)
{
$this->file_search = $file_search; //string that the user put in to search for
$this->original_path = $path; //directory to start searching in (string)
$this->original_dir = opendir($this->original_path);
$paths = $this->getNames($this->original_dir,$this->original_path);
$this->scanPaths($this->original_dir,$paths);
for($x=0;$x<count($this->files);$x++)
{
$this->doesItMatch($file_search,$this->files[$x]);
}
return $this->match_files;
}
///////////////////////////////////
//This function gets the path names of all files and folders in the current directory
//then stores the names to an array.
///////////////////////////////////
function getNames($dir,$path)
{
$paths = array();
for($file = readdir($dir); $file != false; $file = readdir($dir))
{
if(!($this->isItDot($file)))
{
$paths[count($paths)] = $path."/".$file;
}
}
return $paths;
}
///////////////////////////////////
//For every file/folder in the current directory, evaluate it. If it is a directory,
//open it up and get the names again and keep evaluating.
///////////////////////////////////
function scanPaths($dir,$paths)
{
for($x = 0; $x < count($paths); $x++)
{
$it_is_dir = $this->isItDir($paths[$x]);
if($it_is_dir)
{
$nextfolder = $paths[$x].$file;
$nextdir = opendir($nextfolder);//$this->dirs[count($this->dirs)] = opendir($nextpath);
$nextpaths = $this->getNames($nextdir,$nextfolder);
$this->scanPaths($nextdir,$nextpaths);
}
elseif(!$it_is_dir)
{
$this->files[count($this->files)] = $paths[$x];
echo "$file";
}
}
}
///////////////////////////////////
//Is $file_search in the file name? if so store it to the matched files array.
///////////////////////////////////
function doesItMatch($file_search,$path)
{
$pos = strrpos($path,"/");
$file = substr($path,$pos);
if(eregi($file_search,$file))
{
$this->match_files[count($this->match_files)] = $path;
}
}
function isItDir($path)
{
if(filetype($path) == dir)
{
return true;
}
else {return false;}
}
function isItDot($file)
{
if(strcasecmp($file,".") == 0 || strcasecmp($file,"..") == 0)
{return true;}
else {return false;}
}
}
?>
<?php
class Search
{
var $original_path;
var $original_dir;
var $match_files = array();
var $dirs = array();
var $files = array();
var $file_search;
function start($file_search,$path)
{
$this->file_search = $file_search; //string that the user put in to search for
$this->original_path = $path; //directory to start searching in (string)
$this->original_dir = opendir($this->original_path);
$paths = $this->getNames($this->original_dir,$this->original_path);
$this->scanPaths($this->original_dir,$paths);
for($x=0;$x<count($this->files);$x++)
{
$this->doesItMatch($file_search,$this->files[$x]);
}
return $this->match_files;
}
///////////////////////////////////
//This function gets the path names of all files and folders in the current directory
//then stores the names to an array.
///////////////////////////////////
function getNames($dir,$path)
{
$paths = array();
for($file = readdir($dir); $file != false; $file = readdir($dir))
{
if(!($this->isItDot($file)))
{
$paths[count($paths)] = $path."/".$file;
}
}
return $paths;
}
///////////////////////////////////
//For every file/folder in the current directory, evaluate it. If it is a directory,
//open it up and get the names again and keep evaluating.
///////////////////////////////////
function scanPaths($dir,$paths)
{
for($x = 0; $x < count($paths); $x++)
{
$it_is_dir = $this->isItDir($paths[$x]);
if($it_is_dir)
{
$nextfolder = $paths[$x].$file;
$nextdir = opendir($nextfolder);//$this->dirs[count($this->dirs)] = opendir($nextpath);
$nextpaths = $this->getNames($nextdir,$nextfolder);
$this->scanPaths($nextdir,$nextpaths);
}
elseif(!$it_is_dir)
{
$this->files[count($this->files)] = $paths[$x];
echo "$file";
}
}
}
///////////////////////////////////
//Is $file_search in the file name? if so store it to the matched files array.
///////////////////////////////////
function doesItMatch($file_search,$path)
{
$pos = strrpos($path,"/");
$file = substr($path,$pos);
if(eregi($file_search,$file))
{
$this->match_files[count($this->match_files)] = $path;
}
}
function isItDir($path)
{
if(filetype($path) == dir)
{
return true;
}
else {return false;}
}
function isItDot($file)
{
if(strcasecmp($file,".") == 0 || strcasecmp($file,"..") == 0)
{return true;}
else {return false;}
}
}
?>