确实用递归的方式最直接这边有个类你看一下,或许有帮助!<? /************************************ Name: ListDir Author: Riyao Chen riyao83#yahoo.com.cn Version: 0.0.1 Date: 2006-05-16 License: GPL //This class is to list all file & directory follow a directory //Parameter:$strdir ,which directory you want list; $del_url,the manage URL, *********************************/ class ListDir{ var $dir; var $edit_url=""; var $del_url="";
var $num = 0; function ListDir($strdir,$del_url="",$edit_url="") { $this->dir = $strdir; $this->del_url = $del_url; $this->edit_url = $edit_url; }
function MakeDir($StrDir) { //global $num; if (is_dir($StrDir)) { if ($dh = @opendir($StrDir)) { while (($file = readdir($dh)) !== false) {
/************************************
Name: ListDir
Author: Riyao Chen riyao83#yahoo.com.cn
Version: 0.0.1
Date: 2006-05-16
License: GPL
//This class is to list all file & directory follow a directory
//Parameter:$strdir ,which directory you want list; $del_url,the manage URL,
*********************************/
class ListDir{ var $dir;
var $edit_url="";
var $del_url="";
var $num = 0; function ListDir($strdir,$del_url="",$edit_url="")
{
$this->dir = $strdir;
$this->del_url = $del_url;
$this->edit_url = $edit_url;
}
function MakeDir($StrDir)
{
//global $num;
if (is_dir($StrDir)) {
if ($dh = @opendir($StrDir)) {
while (($file = readdir($dh)) !== false) {
$newdir = $StrDir.$file;
if($file == "." || $file == "..")
{
continue;
}
$this->num = $this->num + 1;
print str_repeat(" ",$this->num);
if(filetype($newdir)=="dir")
{
print "<br>";
print "$file [目录]<br>\n";
}
else
{
print "<a href='".$this->del_url."?file=".$newdir."'>EDIT</a> <a href='".$newdir."'>$file</a> <br>\n";
}
if(is_dir($newdir))
{
$this->MakeDir($newdir."/");
}
$this->num = $this->num - 1;
}
print "<br>";
closedir($dh);
}
} }
function ShowDir()
{
$this->MakeDir($this->dir);
}
}/*
exempleinclude_once("./class.dir.php");
$dir = new ListDir("c:/","http://127.0.0.1/index.php");
$dir->ShowDir();*/
?>
function getFile($dir) {
$fileArr = array();
$dp = opendir($dir);
while (($file = readdir($dp)) !== false) {
if ($file !="." && $file !=".." && $file !="") {
if (is_dir($dir."/".$file)) {
$fileArr = array_merge($fileArr, getFile($dir."/".$file));
} elseif (is_file($dir."/".$file)) {
$fileArr[] = $dir."/".$file;
}
}
}
closedir($dp);
return $fileArr;
}
foreach (getFile("home") as $file) {
echo $file."<br>";
}
?>