简体   繁体   English

如何创建搜索文件夹中的文件的搜索表单?

[英]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.

相关问题 如何完成在文件夹中显示文件的搜索表单的代码? - How do I finish code for search form that displays files in a folder? 如何创建这个简单的搜索表单? - How can I create this simple search form? 如何在搜索引擎上检测准确的搜索 - How can I detect exact searches on my search engine 如何创建一个使用标签,类别或标题仅搜索媒体库中视频的wordpress搜索表单 - How to create a wordpress search form that searches only videos in the media library using tag, category, or title 如何使搜索栏仅搜索我的网站页面并提供建议 - How can I make search bar searches only my website pages and give suggetions 如何在Magento搜索中重置addAttributeToFilter - How can I reset addAttributeToFilter in Magento searches 如何制作“频繁搜索”引擎? - How can I make a “frequent searches” engine? 如何使用PHP创建一个文件夹,其名称基于HTML表单的输入? - How can I create an folder using PHP with the name based on the input of an HTML Form? 如何创建一个 VSCode 片段以在我的 src 文件夹中自动插入文件的命名空间? - How can I create a VSCode snippet to automatically insert namespace of files inside my src folder? 如何在php中创建子文件夹 - How can I create a sub folder in php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM