我一直在寻找解决我的问题的方法。我有一个网站,在许多子目录中有许多文件(都在一个主目录中)。我需要一个搜索功能,可以搜索文件名的一部分并将其返回。我需要该函数不区分大小写(即,如果文件名全大写并且用户使用小写搜索,那么它仍然应该找到该文件)。我在下面的功能仅适用于 1 个目录,不能递归工作。
请帮我修改我的代码以使用多个目录。
<?php
$dirname = "./OFFICE PRECEDENTS/";//Directory to search in. *Must have a trailing slash*
$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.
//Display the search results as links.
echo'<a href="' . $dirname . $file . '">' . $file . '</a><br>';
}else{
//Leave this blank
}
}
}
if (!isset($thereisafile)){
echo "Nothing was found.";//Tell the user nothing was found.
echo '<img src="yourimagehere.jpg"/>';//Display an image, when nothing was found.
}
?>