【发布时间】:2013-12-29 21:29:31
【问题描述】:
我的网站上有搜索功能。我希望它在某个文件夹中搜索我服务器上的文件并从那里显示结果。我宁愿不使用数据库。
有没有办法做到这一点?
【问题讨论】:
-
问题太笼统了,能详细点吗?
-
几秒钟后....
标签: php html search-engine
我的网站上有搜索功能。我希望它在某个文件夹中搜索我服务器上的文件并从那里显示结果。我宁愿不使用数据库。
有没有办法做到这一点?
【问题讨论】:
标签: php html search-engine
<?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);
}
}
?>
主要来自 php.net。显然改变你的文件路径。还将中间的 $_POST 命令更改为搜索框输入的名称。这只会找到与您的搜索完全匹配的内容。可以进行一些更改以找到相近的匹配项。
【讨论】:
$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);
}
}
我从PHP.net 得到这个。希望对您有所帮助。
【讨论】:
我就是这样做的,尽管它显示了该目录中的所有文件,并且没有给出每个文件的简要描述。不知道能不能帮忙修改一下。
<?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");
?>
【讨论】: