我希望一次只遍历1级目录,比如第一次遍历后出现A目录,点击A目录后,输出A目录的子文件夹,以此类推
我现在做了2个PHP页,如果是多级目录该怎么写
****
if (is_dir($path))
{
if ($dh = opendir($path)) {
if($st)
{
$re= "<ul id='dhtmlgoodies_tree2' class='dhtmlgoodies_tree'>";
}else{
$re="<ul>";
}
while (false!==($file = readdir($dh)) ) {
$path2 = $path.'/'.$file;
if($file !='.' && $file !='..')
{
$re .= "<li id='node$st' path=$file><a href='ajaxlog.php?path=$file'>".$file."</a>";
$re .= "</li><div id='".$file."'></div>";
}
$st++;
}
$re .= "</ul>";
closedir($dh);
*****
我现在做了2个PHP页,如果是多级目录该怎么写
****
if (is_dir($path))
{
if ($dh = opendir($path)) {
if($st)
{
$re= "<ul id='dhtmlgoodies_tree2' class='dhtmlgoodies_tree'>";
}else{
$re="<ul>";
}
while (false!==($file = readdir($dh)) ) {
$path2 = $path.'/'.$file;
if($file !='.' && $file !='..')
{
$re .= "<li id='node$st' path=$file><a href='ajaxlog.php?path=$file'>".$file."</a>";
$re .= "</li><div id='".$file."'></div>";
}
$st++;
}
$re .= "</ul>";
closedir($dh);
*****
2 然后每个目录绑定通过ajax访问php(带着当前目录的路径)
3 点击目录,触发ajax请求
4 php获取路径
5 php遍历当前目录获取所有子目录
6 有子目录,则子目录处理2的步骤,然后返回内容,重复3步骤
我就是这么写的 可是到ajaxlog.php不知道该怎么写了
$file为文件名$re .= "<li id='node$st' path=$file><a href='ajaxlog.php?path=$file'>".$file."</a>";我另外写了一个ajaslog.php页$PostPath = $_GET["path"];$path = "../../log/".$PostPath."/";
//echo $path;function GetDirInfo($path)
{
$re="";
if (is_dir($path))
{
if ($dh = opendir($path)) {
$re="<ul>";
while (false!==($file = readdir($dh)) ) {
$path2 = $path.'/'.$file;
if($file !='.' && $file !='..')
{
if(is_dir($path2)){
$re .= "<li id='node$st'><a href=''>".$file."</a><a href='ajaxlog.php?path=$file'>".GetDirInfo($path2)."</a>";
}
else{
$re .= "<li class='image'><img src='images/dhtmlgoodies_sheet.gif'><a href='head.php?path=$path2&file=$file'>".$file."</a>";
}
$re .= "<div id='".$file."'></div>";
}
$st++;
}
$re .= "</ul>";
closedir($dh);
}
}
return $re;
}
$pathlist =GetDirInfo($path);
echo $pathlist;
function GetDirInfo($path) {
$re = "";
if(is_dir($path)) {
if($dh = opendir($path)) {
$re = "<ul>";
while(false !== ($file = readdir($dh))) {
$path2 = $path . '/' . $file;
if($file != '.' && $file != '..') {
if(is_dir($path2)) {
// 不明白你这里到底是点击打开呢?还是就直接把子目录、孙子目录、…… 打开呢?
// 如果是需要点击打开,那么就是
$re .= "<li id='node$st'><a href=''>" . $file . "</a><a href='ajaxlog.php?path=$PostPath/$file'>" . $file. "</a>";
// 如果是直接展现的话,那么应该是
$re .= GetDirInfo($path2);
} else {
$re .= "<li class='image'><img src='images/dhtmlgoodies_sheet.gif'><a href='head.php?path=$path2&file=$file'>" . $file . "</a>";
}
$re .= "<div id='" . $file . "'></div>";
}
$st++;
}
$re .= "</ul>";
closedir($dh);
}
}
return $re;
}
我就是要如果为文件夹,就一直点击后遍历,一直到为.txt文件的时候,在点击.txt文件时候下载这个.txt文件
function sc($dir)
{
$temp=scandir($dir);
foreach($temp as $k=>$v)
{
if($v=="."||$v=="..")
{
continue;
}
else
{
if(is_dir($dir."/".$v))
{
$array[$v]=sc($dir."/".$v);
}
else
{
$array[]=$v;
}
}
}
}
传参就行了。
我用你这句写完后
$re .= "<li id='node$st'><a href=''>" . $file . "</a><a href='ajaxlog.php?path=$PostPath/$file'>" . $file. "</a>";
写完后再点击子文件夹是空的呢
function GetDirInfo($path)
{
$re="";
global $st;
if (is_dir($path))
{
if ($dh = opendir($path)) {
if($st)
{
$re= "<ul id='dhtmlgoodies_tree2' class='dhtmlgoodies_tree'>";
}else{
$re="<ul>";
}
while (false!==($file = readdir($dh)) ) {
if($file !='.' && $file !='..')
{
$re .= "<li id='node$st' path=$file><a href='ajaxlog.php?path=$file'>".$file."</a>";
$re .= "</li><div id='".$file."'></div>";
}
$st++;
}
$re .= "</ul>";
closedir($dh);
}
}
return $re;
}
这个是ajaxlog.php页面
$PostPath = $_POST["path"];$path = "../../log/".$PostPath;
/*echo $path;*/
$st=1;
function GetDirInfo($path)
{
$re="";
global $st;
if (is_dir($path))
{
if ($dh = opendir($path)) {
if($st)
{
$re= "<ul id='dhtmlgoodies_tree2' class='dhtmlgoodies_tree'>";
}else{
$re="<ul>";
}
while (false!==($file = readdir($dh)) ) {
$path2 = $path.'/'.$file;
if($file !='.' && $file !='..')
{
//$re .= "<li id='node$st'><a href=''>" . $file . "</a><a href='ajaxlog.php?path=$PostPath/$file'>" . $file. "</a>";
$re .= "<li id='node$st' path=$file><a href=''></a><a href=''>".$file."</a>";
$re .= "</li><div id='".$file."'></div>";
}
$st++;
}
$re .= "</ul>";
closedir($dh);
}
}
return $re;
}
$pathlist =GetDirInfo($path);
echo $pathlist;
$re .= "<li id='node$st'><a href=''>" . $file . "</a><a href='ajaxlog.php?path=". urlencode($PostPath.'/'.$file) . "'>" . $file. "</a>";/这个要转义的
同一个页面也可以传参吧,我要是只在log页面中传呢
同一页面确实可以传参,但是你这一句$path = "../../log/".$PostPath; // 红色部分限定了所传参数必须是log的相对路径,所以第一目录的时候,你直接传path=$file,是没有错的但是到ajaxlog.php以后,你就要考虑log的相对路径了!设:log
-a
--b
--c
-d
--e
---f
有如目录结构当第一次的时候 path = d$path = "../../log/".$PostPath; // 执行以后是 ../../log/d,没有错那么如果处理到e的时候呢?难道要path = e?$path = "../../log/".$PostPath; // 执行以后是 ../../log/e,肯定错,没有这样的路径那就要是 path = d/e$path = "../../log/".$PostPath; // 执行以后是 ../../log/d/e,正确所以你处理的时候才会有$re .= "<li id='node$st'><a href=''>" . $file . "</a><a href='ajaxlog.php?path=". urlencode($PostPath.'/'.$file) . "'>" . $file. "</a>";
不过,这样的话还是要2个php页面啊,我只想在log.php页面里实现,ajaxlog.php不需要了
我直接也可以这么$re .= "<li id='node$st'><a href='log.php?path=". urlencode($PostPath.'/'.$file) . "'>" . $file. "</a>";么,可是同一个页面怎样获取到这个参数呢
你加我Q吧,咱QQ上说 330269414
{
if ($file != '.' && $file != '..') {
if(is_dir($path)) {
// 如果是需要点击打开,那么就是
$re .= "<li id='node$st'><a href=''></a><a href='log.php?path=". urlencode($PostPath.'/'.$file) . "'>" . $file. "</a>";
} else {
$re .= "<li><a href=''></a><img src='images/dhtmlgoodies_sheet.gif'><a href='head.php?path=$path2&file=$file'>" . $file . "</a>";
}
}
$re .= "</li>";
}