AJAX和PHP根据需要使用RecursiveIteratorIterator来获取文件和文件夹列表
AJAX and PHP get files and folders list using RecursiveIteratorIterator as needed
我需要从服务器获取文件和文件夹列表。
我不想一次得到所有的文件,文件夹,子文件夹。根据需要,我想一点一点地得到他们的。所以我使用AJAX。
我使用PHP。我使用递归迭代器
我在没有AJAX的情况下做到了,但是我不能使用AJAX:
只适用于第一个目录(~根目录)。我不能在函数中传递$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();