如何完成在文件夹中显示文件的搜索表单的代码?

时间:2018-09-10 01:14:48

标签: php html search

我想要一个搜索表单,用于搜索并显示文件夹中的文件。我在下面找到了参考PHP代码(我完全不了解):

<?php
$dir = "/Users/rosaliaobrien/Desktop/Bio_pages/HTML_files/South";

// 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 == $_???['_???']){
            echo('<a href="'.$dir . $file.'">'. $file .'</a>'."\n");
        }
    }
    closedir($dh);
}
}
?>

我不确定在标记为“ _ ???”的两个空格中插入什么。这是搜索栏的html代码:

<form class="searchbar" action="//Desktop/fields/html\ codes/searchresults.php" style="max-width:300px">
<input type="text" placeholder="Search.." name="search2">
<button type="submit"><i class="fa fa-search"></i></button>

1 个答案:

答案 0 :(得分:0)

这是工作代码, 如果您有abchome.txt之类的文件,则在搜索主目录时,它将显示abchome.txt文件,因此启用了文件名的部分匹配。

<?php

if(!empty($_GET['submit'])){
    $search = trim($_GET['search']);
    $dir = "/Users/rosaliaobrien/Desktop/Bio_pages/HTML_files/South";
    // $dir = "/usr/games/";

    // Open a known directory, and proceed to read its contents
    if (is_dir($dir)) {
        if ($dh = opendir($dir)) {
            while (($file = readdir($dh)) !== false) {

                // here u can use regular expressions also
                if(strpos($file, $search) !== false){
                    echo('<a href="'.$dir . $file.'">'. $file .'</a>'."<br/>");
                }
            }
            closedir($dh);
        }
    }
}
?>


<form class="searchbar" action="" style="max-width:300px">
    <input type="text" placeholder="Search.." name="search">
    <button type="submit" name="submit" value="1"><i class="fa fa-search"></i>submit</button>
</form>