我有一个显示我的本地文件列表的网页,我有一个搜索栏,它会浏览文件列表并突出显示第一个匹配项。
但是,如何仅在用户搜索文件名时才显示文件。因此,我不是要显示所有文件,而只是希望返回符合搜索条件的文件。
PHP,JavaScript,jQuery完全是一个选项,如果有人可以在这方面提供帮助。
testexec.php:
<?php
$path = '/var/www/html/'; //get list of files
$files = scandir($path);
//display the links
foreach($files as $file) {
if($file != '.' && $file != '..') {
echo '<div><a href="readfile.php?file='.urlencode($file).'"> '.$file.'</a></div>';
}
}
?>
readfile.php:
<?php
// PHP script to allow the file to be downloaded
$filename = $_GET['file'];
if (file_exists($filename)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment;
filename="'.basename($filename).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($filename);
exit;
}
?>
//JavaScript for searchbar function FindNext() { var str = document.getElementById ("livesearch").value; if (str == "") { alert ("Please enter some text to search!"); return; } var supported = false; var found = false; if (window.find) { // Firefox, Google Chrome, Safari supported = true; // if some content is selected, the start position of the search // will be the end position of the selection found = window.find (str); } else { if (document.selection && document.selection.createRange) { // Internet Explorer, Opera before version 10.5 var textRange = document.selection.createRange (); if (textRange.findText) { // Internet Explorer supported = true; // if some content is selected, the start position of the search // will be the position after the start position of the selection if (textRange.text.length > 0) { textRange.collapse (true); textRange.move ("character", 1); } found = textRange.findText (str); if (found) { textRange.select (); } } } } if (supported) { if (!found) { alert ("The following text was not found:\n" + str); } } else { alert ("Your browser does not support this example!"); } }
答案 0 :(得分:1)
这是最简单的想法。
的index.html
$('input').keydown(function(e) {
var str = $(this).val();
alert(str);
$.get("/search.php?query=" + str, function(data) {
$('.result').html(data);
});
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h3>File search and download</h3>
<input name="filename" placeholder="file name" class="kw"/>
<div class="result">
</div>
&#13;
的search.php
<?php
// You need code search file
// after search $files
$str = '';
foreach($files as file) {
$str .= '<a href="readfile.php?file=...">'.$file.'</a> <br>'
}
return $str;
?>
readfile.php
<?php
// PHP script to allow the file to be downloaded
$filename = $_GET['file'];
if (file_exists($filename)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment;
filename="'.basename($filename).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($filename);
exit;
}
?>
答案 1 :(得分:0)
有很多方法可以做到这一点。
我建议:
使用符合给定条件的文件让您的PHP回答JSON。所以你会问PHP,传入POST数据ou QUERY字符串&#34; text&#34;那就是搜索。它只会为您提供匹配的文件。
在你的html文件中(也可能是另一个PHP),每次用户更改搜索文本时,你都会调用ajax(你可以使用jQuery)到上面的页面。咳嗽&#34;这是件好事。 (参见lodash / underscore库)(等待一些时间等待更多按键)。
收到匹配文件的JSON后,动态构建表格(或其他您想要的方式)。
的search.php:
<?php
header('Content-Type: application/json');
$path = '/var/www/html/'; //get list of files
$files = scandir($path);
$search = $_GET['search'];
$links = array();
foreach ($files as $file) {
if($file != '.' && $file != '..' && strpos(strtolower($file), strtolower($search)) !== false) {
array_push($links, array(
"name" => $file,
"url" => "readfile.php?file=" . urlencode($file)
));
}
}
echo json_encode($data);
?>
index.php / index.html
<html>
<head>
<script src="http://code.jquery.com/jquery-2.2.4.min.js">
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js">
</head>
<body>
<input id="searchbox" type="text" placeholder="search for files" />
<div id="results">
</div>
<script>
$(function () {
var searchbox = $("#searchbox");
var results = $("#results");
var doSearch = _.throttle(function () {
var searchtext = searchbox.val();
$.ajax({
type: 'get',
url: 'search.php',
dataType: "json",
data: {
search: searchtext
}
}).done(function (response) {
results.html(response.reduce(function (html, item) {
return html + '<div><a href="' + item.url + '">' + item.name + '</a></div>';
}, ''));
});
}, 200);
searchbox.on('keydown', doSearch);
});
</script>
</body>
</html>
答案 2 :(得分:0)
<?php
$path = 'C:/xampp/htdocs/';
$keyword = isset($_POST['keyword']) ? $_POST['keyword'] : '';
$scan = scandir($path);
$result = array('ok'=>0); //prepare output cz we will use json instead text/html
if($scan !== false){
$result['ok']=1;
$list = array();
foreach($scan as $file){
if(is_file($path.$file)){ //only file
if(preg_match('/'.$keyword.'/', $file)) //is file containts keyword?
$list[] = '<div><a href="readfile.php?file='.$file.'">'.$file.'</a></div>';
}
}
$result['list'] = count($list) == 0 ? 'no file match': $list;
}else
$result['msg'] = "failed open dir";
echo json_encode($result);
和一些代码修复
的index.php
<?php
// PHP script to allow the file to be downloaded
$filename = $_GET['file'];
$path = 'C:/xampp/htdocs/';
$fullPath = $path.$filename; //you need this
if (file_exists($fullPath)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($filename).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($fullPath));
readfile($fullPath);
exit;
}
?>
的search.php
sucess
readfile.php
success