?
It is convenient to call this class to view all the files in the specified directory in a tree structure, if you want to know what subdirectories and files are in a directory.
# Examples of demonstrations:
$t = new Treeclimber ("ASP"); Create a new object to set the directory you want to list: This is the ASP directory
Echo arrayvaluestostring ($t->getfilelist ($t->getpath ()), "<br>\n");
function arrayvaluestostring ($ar, $nl = "", $dolast =true) {//calling functions
$str = "";
Reset ($ar);
$size = sizeof ($ar);
$i = 1;
while (the list ($k, $v) = each ($ar)) {
if ($dolast = = False) {
if ($i < $size) {
$str. = $ar [$k]. $nl;
}
else {
$str. = $ar [$k];
}
}
else {
$str. = $ar [$k]. $nl;
}
$i + +;
}
return $str;
}
?>
?
The following are class files
Class Treeclimber {
var $path;
var $fileList = array ();
function Treeclimber ($path = ".") {
$this->path = $path;
}
# Access Path
function GetPath () {return $this->path;}
function SetPath ($v) {$this->path = $v;}
Returns a list of files in the specified directory, using the current directory if no directory is specified
If the directory cannot be opened (there may not be a permission or the directory does not exist, it will return empty
In a recursive manner
function Getfilelist ($dirname =null, $returnDirs =false, $reset =true) {
if ($dirname = = null) {$dirname = $this->path;}
# else {$this->setpath ($dirname);}
# dout ("recursing into $dirname ...");
if ($reset) {
$this->filelist = Array ();
}
$dir = Opendir ($dirname);
if (! $dir) {
Print ("<b><font color= #FF0000 > Note: treeclimber.getfilelist ($dirname): Cannot open $dirname!</font></b > ");
return null;
}
while ($file = Readdir ($dir)) {
if (Ereg ("^\.$", $file) | | ereg ("^\.\.$", $file)) continue;
if (Is_dir ($dirname.) /". $file)) {
$this->getfilelist ($dirname.) /". $file, $returnDirs, false);
if ($returnDirs) {$this->filelist[] = $dirname. " /". $file;}
}
else {$this->filelist[] = $dirname. " /". $file; }
}
Sort ($this->filelist);
return $this->filelist;
}
}//End of class
?>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.