indexing and searching

hi guys im looking for a script that will allow users to enter a keyword in an html form which will then be used as a search word within a directory so if the person searches mp3 it would return a list of files named containing the NAME mp3. ive found a script online that does this but it has one problem - it only searches for content within the root folder and doesnt include sub directories.

any help would be greatly appreciated ( i have no PHP knowledge )

[php]<?php
//////////////////////
// © Nadav Ami 2009 //
// Version 1.2 //
//////////////////////

$dirname dir(".");
$findme = $_POST[“search”];

$dir = opendir($dirname);

while(false != ($file = readdir($dir))){//Loop for every item in the directory.
if(($file != “.”) and ($file != “…”) and ($file != “.DS_Store”) and ($file != “search.php”))//Exclude these files from the search
{
$pos = stripos($file, $findme);
if ($pos !== false){
$thereisafile = true;//Tell the script something was found.
echo’’ . $file . ‘
’;//Display the search results as links.
}else{
//Leave this blank
}
}
}
if (!isset($thereisafile)){
echo “Nothing was found.”;//Tell the user nothing was found.
echo ‘’;//Display an image, when nothing was found.
}
?>

[/php]

Well, a short jump to the net to remember a command that I seldom use found that you can do your search
in what is called a “recursive” search thru your folders in just a few commands. You have to use a command
called “RecursiveDirectoryIterator”. You could use the scandir or other such commands, but, this one seems
to be exactly what you are looking for. Here is a link to the PHP.NET site to explain it further. Note the
programmers comments down the page a bit that explain various things you can do with this command.
http://php.net/manual/en/class.recursivedirectoryiterator.php

So, really only two lines of code is needed! Here is the basic layout of how it works…
[php]
// Set up the target directory ( top directory or folder, sub-folders will be parsed )
$directory = new RecursiveDirectoryIterator( DIR . ‘/…’, RecursiveDirectoryIterator::KEY_AS_FILENAME | RecursiveDirectoryIterator::CURRENT_AS_FILEINFO);

// Now iterate thru the folder and sub-folders to locate all files…
$files = new RegexIterator( new RecursiveIteratorIterator($directory), ‘#^file.txt$#’, RegexIterator::MATCH, RegexIterator::USE_KEY );

// For example display each of them…
foreach($files as $file){
echo $file . “\n”;
}
[/php]
NOTE: This sample displays all files that are named “file.txt”! You would have to alter your Regex string to
look in the filename for your keyword. This would be much faster than removing the regex and using just
PHP functions. This code combines the folder recursion and the search into one funciton. I think that the
correct regex would be something like: ‘/yourstring/’ so to fix that up you should replace the ‘#^file.txt$#’
with something like: ‘/$findme/’ or maybe ‘/’ . $findme . ‘/’ Should work…

You can basically use this code to search for just about anything that you can create a REGEX for. This is
of course a “Regular Expression” and there are thousands of sites online with huge lists of the common
ones that might be helpful for other parts of your project.

Hope this helps! Good luck…

One further note on this function. You can use the fileinfo() to get more info about each file.
Also, since the output is in an array format, it is very easy to alter this to keep any other data
and to display it in various easy ways. You just have to parse thru the array and pull out the
data for each file.

To check if any files were found, just count the array and see if it is zero. ( count($files) )

Thanks for the reply and the help Ernie. I managed to find a completed script online that had the recursive search implemented.

Good, Dmarshall…

Normally if you find a solution, you should post it for others to use or at least post that it was solved.
So that others do not waste time creating a solution for you when you already have one. I will mark
this solved.

If you are still using your old code version, you might want to change it to something like mine as it is
server-based and should be much faster than looping thru folders in PHP. Timing tests on PHP.net say
it is 30 to 90 times faster… Guess that depends on how big your sub-folders list is. Recursion is slow
under a lot of servers.

Well, glad you solved it…

Sponsor our Newsletter | Privacy Policy | Terms of Service