I am trying to search a directory (upload) for file names that match the search form.
I enter the word 'Bach' into the search field on my website. I would then like my script to search through all the files in the directory and display all of the files of which contain the word 'bach'.
This is the code that I have come up with, of which I expected it to work. I cannot see any visual errors and was wondering if you could help me.
At the moment, I am getting no results from the search.
if(isset($searchvalue))
{
$dir = opendir('upload/');
while ($read = readdir($dir))
{
if ($read!='.' && $read!='..')
{
if (stristr($searchvalue,$read)) //case insensitive
{
echo '<p><a href="upload/'.$read.'">'.$read.'</a></p>';
}
}
}
closedir($dir);
}
if (isset($searchvalue)) {
$dir = opendir('upload/');
while ($read = readdir($dir)) {
if ($read != '.' && $read != '..') {
if (stristr($read, $searchvalue)) //case insensitive
{
echo '<p><a href="upload/' . $read . '">' . $read . '</a></p>';
}
}
}
closedir($dir);
}
Fix: Remove the quotes and turn the parameters in stristr()
around. You were searching the filename in the search-input. But you have to search the search-input in the filename. Otherwise you check if "something.txt" is in "something" When you input "something" => evaluates to FALSE