我正在尝试搜索目录(上传)中与搜索表单匹配的文件名。
我在我的网站的搜索栏中输入"Bach"这个词。然后,我希望我的脚本搜索目录中的所有文件,并显示包含单词'bach'的所有文件。
这是我想出来的代码,我希望它能工作。我看不出任何视觉错误,不知道你能否帮助我。
目前,我没有得到任何结果从搜索。
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);
}
你不应该在stristr调用中引用参数。
试试这个:
if (stristr($searchvalue,$read))
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);
}
修正:删除引号,并把参数在stristr()
周围。您在搜索输入中搜索文件名。但是你必须搜索文件名中的搜索输入。否则检查"something.txt"是否在"something"中当你输入"something" =>计算结果为FALSE