在服务器上搜索文件夹并显示结果

时间:2014-12-08 22:20:16

标签: javascript php html html5

如何搜索服务器上的文件夹并在我的网页上显示结果?我在How can I create a search form that searches files in a folder?找到了一个类似的问题,但我无法弄清楚如何将php脚本连接到我的html:

<!DOCTYPE html>
<html>
<body>

<div>
<input id="query" type="text"/><button id="search-button"  onclick="?????">Search</button>
</div>

<script>
var q=document.getElementById("query");



</script>


<?php
$dir = "/uploads/";

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

</body>
</html>

1 个答案:

答案 0 :(得分:0)

您可以使用jQuery的post方法无缝地(异步地)将信息发布到php脚本。然后,您可以将数据返回到调用页面并显示它。

所以有php脚本列出/找到文件和html页面分开。然后在html页面中使用javascript和jQuery发布到那个php finder脚本。类似的东西:

$.post('phpFileFinder.php', {fileName: fileNameJSvar}, function(data){

        $("#divId").html(data);
 });

然后在你的PHP脚本中你可以:

$dir = $_POST['fileName'];
$fileArray = array();
// 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['q']){
        array_push($fileArray,'<a href="'.$dir . $file.'">'. $file .'</a>'."\n");
    }
}
closedir($dh);
}

print_r($fileArray);
}
相关问题