我有一个显示我的本地文件列表的网页,我有一个搜索栏,它遍历文件列表并突出显示第一个匹配项。
但是,如何才能仅在用户搜索文件名时显示文件。因此,我不想显示所有文件,而是只希望返回符合搜索条件的文件。
如果有人能在这方面提供帮助,PHP、JavaScript、jQuery 完全是一个选择。
测试执行器.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>';
}
}
?>
读取文件.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!"); } }
最佳答案
这是最简单的想法。
前端
index.html
$('input').keydown(function(e) {
var str = $(this).val();
alert(str);
$.get("/search.php?query=" + str, function(data) {
$('.result').html(data);
});
});
<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>
后端
搜索.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;
?>
读取文件.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 搜索栏在用户输入后显示结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48946243/