树型结构列出指定目录里所有文件的PHP类
时间:2023-11-17 04:49:04
<?
//以树型结构列出指定目录里的所有文件,如果你想知道自己某个目录里有哪些子目录和文件,可以调用这个类来查看,很方便的。
# 演示的例子:
$t = new TreeClimber( "asp" ); //新建物件,设置需要列出的目录:在此为asp目录
echo arrayValuesToString( $t->getFileList( $t->getPath() ), "<BR>\n" );
function arrayValuesToString( $ar, $nl="", $dolast=true ) {//调用函数
$str = "";
reset( $ar );
$size = sizeof( $ar );
$i = 1;
while( 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;
}
?>
<?
//以下为类文件
class TreeClimber {
var $path;
var $fileList = array();
function TreeClimber( $path = "." ) {
$this->path = $path;
}
# 存取路径
function getPath() { return $this->path; }
function setPath( $v ) { $this->path = $v; }
// 返回指定目录里的文件列表,如果没有指定目录,将使用当前目录
//如果不能打开目录(可能没权限或目录不存在,将返回为空
//以递归方式进行
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>注意: TreeClimber.getFileList( $dirname ): 不能打开 $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;
}
} //至此类结束
?>
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
YUV转为jpg图像的实现
python GUI库图形界面开发之PyQt5动态加载QSS样式文件
![](https://img.aspxhome.com/file/2023/5/118585_0s.png)
js版sliderBar(滑动条)控件
Python的条件语句与运算符优先级详解
![](https://img.aspxhome.com/file/2023/9/121709_0s.jpg)
python 获取字典特定值对应的键的实现
JavaScript 编程引入命名空间
Go语言Mock使用基本指南详解
![](https://img.aspxhome.com/file/2023/5/128665_0s.jpg)
perl文件读取的几种处理方式小结
R语言绘制条形图及分布密度图代码总结
![](https://img.aspxhome.com/file/2023/8/118618_0s.png)
python之 matplotlib和pandas绘图教程
![](https://img.aspxhome.com/file/2023/5/133775_0s.jpg)
SQLSERVER ISNULL 函数与判断值是否为空的sql语句
windows环境下mysql的解压安装及备份和还原
![](https://img.aspxhome.com/file/2023/5/75635_0s.png)
Matplotlib绘制子图的常见几种方法
![](https://img.aspxhome.com/file/2023/8/102338_0s.png)
python练习之循环控制语句 break 与 continue
![](https://img.aspxhome.com/file/2023/1/86331_0s.png)
解决PHP 7编译安装错误:cannot stat ‘phar.phar’: No such file or directory
![](https://img.aspxhome.com/file/2023/2/55632_0s.jpg)
python深度学习tensorflow卷积层示例教程
在javascript中如何取整?
python Django连接MySQL数据库做增删改查
python去除文件中空格、Tab及回车的方法
如何使用Django默认的Auth权限管理系统
![](https://img.aspxhome.com/file/2023/0/134880_0s.png)