如何创建一个搜索表单来搜索文件夹中的文件? [英] How can I create a search form that searches files in a folder?

查看:69
本文介绍了如何创建一个搜索表单来搜索文件夹中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站上有搜索功能.我希望它在我的服务器上的某个文件夹中搜索文件并从那里显示结果.我宁愿不使用数据库.

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);
    }
}
?>

主要来自 php.net.显然改变你的文件路径.还将中间的 $_POST 命令更改为搜索框输入的任何名称.这只会找到与您的搜索完全匹配的内容.可以进行一些更改以找到接近的匹配项.

From php.net mostly. Obviously change your file path. Also change the $_POST command in the middle to whatever the name of your search box input is. This will only find exact matches of your search. Some changes could be made to find close matches.

这篇关于如何创建一个搜索表单来搜索文件夹中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆