问题描述
我需要从服务器获取文件和文件夹列表.
我不想一次获取所有文件,文件夹,子文件夹.
我想根据需要一点一点地得到他们的.所以我用AJAX.
I need get list of files and folders from server.
I don't want get all files, folders, sub-folders at once.
I want get theirs little by little, as needed. So I use AJAX.
我使用PHP.我使用RecursiveIteratorIterator.
I use PHP. I use RecursiveIteratorIterator.
我没有AJAX做到了,但是使用AJAX 无法做到:
I did it without AJAX, but I can't do it using AJAX:
它仅适用于第一个目录(〜根目录).我无法在函数中传递$ name变量.
It works only for first directory (~root directory). I can't pass $name variable in function.
**不是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.)
推荐答案
它仅适用于您的根目录的原因是:
The reason it only works for your root directory is because of this:
print "<a href='?you_choose_directory=" . basename($name) . "'><b>" . basename($name) . "</b></a>";
您正在链接到您所在的html页面,因此该页面将重新加载,一切重新开始.您需要将该标签的href设置为javascript:void,以免重新加载页面.然后,您需要添加一个onClick事件处理程序以调用do_it()函数,并将其传递到要列出其内容的目录的路径.您将需要更改do_it()以便它可以接受参数.
You are making a link to the html page you are on, so the page reloads and everything starts over. You need to set the href of that a tag to javascript:void so the page doesn't reload. Then you need to add an onClick event handler to call your do_it() function and pass it the path to the directory whose contents you want to list. You will need to change do_it() so that it can accept a parameter.
HTML和javascript:
HTML and 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>";
}
}
}
?>
这篇关于AJAX和PHP根据需要使用RecursiveIteratorIterator获取文件和文件夹列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!