Jump to content

Directory search script


Dustin Lee

Recommended Posts

Hello all, I have been working on a search script for my site. I currently have everything upload to a directory so a directory, so I'm stuck there.My main problem is, I can only set it to search only one directory at any time, and any attempts I have to have it search multiples at once is stopped with errors.

<?phpinclude "header.php";$upload_dir = "visual/files/";$handle = opendir($upload_dir);$name = str_replace(" ","",$name);if($_GET[s]) {	while ($file = readdir($handle)) {		if(!is_dir($file) && !is_link($file)) {			if(preg_match("/".$_GET[s]."/i", $file)) {				$filelist .= "<a href='$upload_dir$file'><b>$file</b></a><br />";			}		}	}}if($filelist) {	echo "<b>Search results:</b><br /><br />";	echo $filelist;} else {	echo "<b>No results found!</b>";}include "footer.php";?>

I have looked at a few methods, but none seem to work.How would I go about having it simultaneously search several different directories?

Link to comment
Share on other sites

You might want to consider using grep, invoked with backticks, system, or exec. Be sure to sanitize any search inputs.grep -ri 'search string' * That would be to search for 'search string' within files. To search for files by name, you could use find - it's another command under Linuxfind -name 'search string'orPHP's glob function, which I'm not sure would work in that way.

Link to comment
Share on other sites

I don't know if you are still looking in to PHP directory functions, but I have a script I made like 2-3 years ago (back when I was a bad coder) that works well. I would not suggest using the script as it probably is inefficient or may abuse resources (there's a reason that you need a password :)). Anyway it uses scandir(), then it looks through that list testing if each element in the array is a file, or directory. If its a directory then it appends that to a list of directories to loop through when the current is finished. This might not work well if you don't have a small simple site, but again use the methods to rewrite your own.

<?phpsession_start();if(!isset($_SESSION['tmp']['auth'])){  if($_SERVER['QUERY_STRING'] == 'clonetrooper9494')    $_SESSION['tmp']['auth'] = true;  else  {    echo 'Please enter in the correct code to continue searching.<br>';    echo '(Type in the correct code after the ? in the url)';    exit;  }}$searchterm = urldecode($_SERVER['QUERY_STRING']);$base_dir = $_SERVER['DOCUMENT_ROOT'];//$base_dir = getcwd();$dir = '/home/clonedro/';//$base_dir.'/';$file = scandir($dir);$search_limit = 101;$folders[0] = 'null';$found[0] = 'Found:';foreach($file as $key => $name){  if(count($found) < $search_limit && $name != '.' && $name != '..')  {//    echo $dir.$name.' <- Starting';    if(is_dir($dir.$name))    {      array_push($folders,$dir.$name);//      echo ' <- Is a Folder<br>';    }    else    {//      echo ' <- Is a File<br>';      if(strpos(file_get_contents($dir.$name),$searchterm) !== false || strpos($name,$searchterm) !== false)      {        array_push($found,$dir.$name);        echo $dir.$name.' <- Found Search Term<br>';      }    }  }}while(count($folders) > 1)foreach($folders as $key22 => $name22){if($name22 != 'null' && $name22 != ''){  $file = scandir($name22);foreach($file as $key => $name){  if(count($found) < $search_limit && $name != '.' && $name != '..')  {//    echo $name22.'/'.$name.' <- Starting';    if(is_dir($name22.'/'.$name))    {      array_push($folders,$name22.'/'.$name);//      echo ' <- Is a Folder<br>';    }    else    {//      echo ' <- Is a File<br>';      if(strpos(file_get_contents($name22.'/'.$name),$searchterm) !== false || strpos($name,$searchterm) !== false)      {        array_push($found,$name22.'/'.$name);        echo $name22.'/'.$name.' <- Found Search Term<br>';      }    }  }}}  unset($folders[$key22]);}?>

Link to comment
Share on other sites

You might want to consider using grep, invoked with backticks, system, or exec. Be sure to sanitize any search inputs.grep -ri 'search string' * That would be to search for 'search string' within files. To search for files by name, you could use find - it's another command under Linuxfind -name 'search string'orPHP's glob function, which I'm not sure would work in that way.
I'm not familiar with that code at all - I'm more of a web design type of person.I'll look into it, but this is for website directory searching, right? A few people thought I was talking about searching certain areas of my computer's HDD.
Link to comment
Share on other sites

I don't know if you are still looking in to PHP directory functions, but I have a script I made like 2-3 years ago (back when I was a bad coder) that works well. I would not suggest using the script as it probably is inefficient or may abuse resources. Anyway it uses scandir(), then it looks through that list testing if each element in the array is a file, or directory. If its a directory then it appends that to a list of directories to loop through when the current is finished. This might not work well if you don't have a small simple site, but again use the methods to rewrite your own.
<?phpsession_start();if(!isset($_SESSION['tmp']['auth'])){  if($_SERVER['QUERY_STRING'] == 'clonetrooper9494')    $_SESSION['tmp']['auth'] = true;  else  {    echo 'Please enter in the correct code to continue searching.<br>';    echo '(Type in the correct code after the ? in the url)';    exit;  }}$searchterm = urldecode($_SERVER['QUERY_STRING']);$base_dir = $_SERVER['DOCUMENT_ROOT'];//$base_dir = getcwd();$dir = '/home/clonedro/';//$base_dir.'/';$file = scandir($dir);$search_limit = 101;$folders[0] = 'null';$found[0] = 'Found:';foreach($file as $key => $name){  if(count($found) < $search_limit && $name != '.' && $name != '..')  {//    echo $dir.$name.' <- Starting';    if(is_dir($dir.$name))    {      array_push($folders,$dir.$name);//      echo ' <- Is a Folder<br>';    }    else    {//      echo ' <- Is a File<br>';      if(strpos(file_get_contents($dir.$name),$searchterm) !== false || strpos($name,$searchterm) !== false)      {        array_push($found,$dir.$name);        echo $dir.$name.' <- Found Search Term<br>';      }    }  }}while(count($folders) > 1)foreach($folders as $key22 => $name22){if($name22 != 'null' && $name22 != ''){  $file = scandir($name22);foreach($file as $key => $name){  if(count($found) < $search_limit && $name != '.' && $name != '..')  {//    echo $name22.'/'.$name.' <- Starting';    if(is_dir($name22.'/'.$name))    {      array_push($folders,$name22.'/'.$name);//      echo ' <- Is a Folder<br>';    }    else    {//      echo ' <- Is a File<br>';      if(strpos(file_get_contents($name22.'/'.$name),$searchterm) !== false || strpos($name,$searchterm) !== false)      {        array_push($found,$name22.'/'.$name);        echo $name22.'/'.$name.' <- Found Search Term<br>';      }    }  }}}  unset($folders[$key22]);}?>

That's a lot more complicated that I thought it would be... There is no easy want to define two+ separate directories?Looking through some code this guy has, I see something close to what I need but not entirely.
$FILE_EXTS  = array('.psd','.jpg');

That is kind of what I need, but more so, not.

$upload_dir = "audio/q/";

I just need that to look in: audio/q, audio/x, audio/files, and visual/files, but array (which I assumed meant variety - told you I'm a newb) doesn't seem to work, I'd make something like:

$upload_dir  = array('audio/files','audio/x');

and it would just return errors, all though, having just one works fine.Is there really no way to easily do this without pretty much rewriting my week-long code. I'm slow and uneducated with code as it is, something more advances could take me months.

Link to comment
Share on other sites

Use http://us2.php.net/manual/en/function.glob.php to find files by name. It will give you an array and then you can use http://us2.php.net/manual/en/function.preg-grep.php to search the array.The scandir_through function is from the first link.

<?phpfunction scandir_through($dir){	$items = glob($dir . '/*');	for ($i = 0; $i < count($items); $i++) {		if (is_dir($items[$i])) {			$add = glob($items[$i] . '/*');			$items = array_merge($items, $add);		}	}	return $items;}$aJS=scandir_through('thedirectory');$aResults=preg_grep('/searchstring/',$aJS);var_dump($aResults);?>

Equivalent code using a command (probably won't work on a Windows machine). This is probably faster than the above.

<?php$sString=escapeshellarg('*convert*');$sOutput=`find js/* -name $sString`;var_dump($sOutput);?>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...