[英]How can I create a search form that searches files in a folder?
I have a search feature on my website. 我的网站上有搜索功能。 I would like it to search through a certain folder for files on my server and display results from there.
我希望它在某个文件夹中搜索服务器上的文件并从中显示结果。 I'd rather not use databases.
我宁愿不使用数据库。
Is there a way to do this? 有没有办法做到这一点?
<?php
$dir = "/your_folder_here/";
// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if($file == $_POST['SEARCHBOX_INPUT']){
echo('<a href="'.$dir . $file.'">'. $file .'</a>'."\n");
}
}
closedir($dh);
}
}
?>
From php.net mostly. 大部分来自php.net。 Obviously change your file path.
显然更改您的文件路径。 Also change the $_POST command in the middle to whatever the name of your search box input is.
同样,将中间的$ _POST命令更改为搜索框输入的名称。 This will only find exact matches of your search.
这只会找到您搜索的完全匹配项。 Some changes could be made to find close matches.
可以进行一些更改以找到接近的匹配项。
This is how I did it, although it displays all the files which are in that directory and it does not give a brief description of each file. 尽管它显示了该目录中的所有文件,但没有给出每个文件的简要说明,但这是我的方法。 I dont know if you can help modify it.
我不知道您是否可以帮助修改它。
<?php
print "<h2>Showing results for $search</h2>";
$dirName="MYBOOKS";
$dp=opendir($dirName);
chdir($dirName);
while ($currentFile !== false) {
$currentFile = readDir($dp);
$theFiles[] = $currentFile;
}
$BookFiles= preg_grep("/pdf$|gif$|png$|jpg$|jed$/", $theFiles);
$output="";
foreach ($BookFiles as $currentFile) {
$output .= <<< Here
<ul>
<li><a href=MYBOOKS/$currentFile>$currentFile</a></li>
</ul>
Here;
}
$fp=fopen("BookIndex.htm","w");
fputs ($fp,$output);
fclose($fp);
readfile ("BookIndex.htm");
?> ?>
$dir = "/etc/php5/";
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if($file == 'songs1.php')
//ur code here ...
}
closedir($dh);
}
}
I got this from PHP.net . 我是从PHP.net获得的 。 I hope this helps you.
我希望这可以帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.