我需要从服务器获取文件和文件夹列表。
我不想一次获取所有文件,文件夹和子文件夹。
我想根据需要一点一点地得到他们的。所以我使用AJAX。
我使用PHP。我使用RecursiveIteratorIterator。
我是在没有AJAX的情况下完成的,但我无法使用AJAX :
它仅适用于第一个目录(~root目录)。我无法在函数中传递$ name变量。
**不是JQuery !!! **
PHP
<?php
if (isset($_GET['you_choose_directory'])){print "do something";}
getList("/start_folder/"); //always(
function getList($name) {
$path = realpath($name);
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::CATCH_GET_CHILD);
foreach($objects as $name => $object){
if (filetype($name) == "dir") {
print "<a href='?you_choose_directory=" . basename($name) . "'><b>" . basename($name) . "</b></a>";
} else {
print "<a><div></div><b>" . basename($name) . "</b></a>";
}
}
}
?>
JAVASCRIPT
function getXmlHttp(){
var xmlhttp;
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
xmlhttp = new XMLHttpRequest();
}
return xmlhttp;
}
function do_it() {
var req = getXmlHttp();
var list = document.getElementById('it_is_files');
req.onreadystatechange = function() {
if (req.readyState == 4) {
if(req.status == 200) {
list.innerHTML = req.responseText;
}
}
}
req.open('GET', 'php.php', true);
req.send(null);
list.innerHTML = 'loading...';
}
do_it();
HTML
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="utf-8"/>
<title>file list</title>
<script src="javascript.js"></script>
</head>
<body onload="doOnLoad();">
<div id="it_is_files">
</div>
</body>
</html>
感谢。
(我不认识PHP。)
答案 0 :(得分:3)
它仅适用于您的根目录的原因是:
print "<a href='?you_choose_directory=" . basename($name) . "'><b>" . basename($name) . "</b></a>";
您正在建立您所在的html页面的链接,因此页面会重新加载并重新开始。您需要将该标记的href设置为javascript:void,以便页面不会重新加载。然后,您需要添加一个onClick事件处理程序来调用do_it()函数,并将路径传递给要列出其内容的目录。您需要更改do_it()以便它可以接受参数。
HTML和javascript:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="utf-8"/>
<title>file list</title>
<script type="text/javascript">
function getXmlHttp(){
var xmlhttp;
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
xmlhttp = new XMLHttpRequest();
}
return xmlhttp;
}
function do_it(dirName) {
var req = getXmlHttp();
var list = document.getElementById('it_is_files');
req.onreadystatechange = function() {
if (req.readyState == 4) {
if(req.status == 200) {
list.innerHTML = req.responseText;
}
}
}
req.open('GET', 'getList.php?you_choose_directory=' + dirName, true);
req.send(null);
list.innerHTML = 'loading...';
}
</script>
</head>
<body onload="do_it('');">
<div id="it_is_files">
</div>
</body>
</html>
PHP:
<?php
$dir = "./"; //default to root directory
if (strlen($_GET['you_choose_directory']) > 0){$dir = $_GET['you_choose_directory'];}
getList($dir);
function getList($name) {
$path = realpath($name);
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::CATCH_GET_CHILD);
foreach($objects as $name => $object){
if (filetype($name) == "dir") {
print "<br/><a href='javascript:void' onClick='do_it(\"" . $path . "/" . basename($name) . "\")'><b>" . basename($name) . "</b></a>";
} else {
print "<br/><a><b>" . basename($name) . "</b></a>";
}
}
}
?>