<?
//以树型结构列出指定目录里的所有文件,如果你想知道自己某个目录里有哪些子目录和文件,可以调用这个类来查看,很方便的。
# 演示的例子:
$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;
}
} //至此类结束
?>
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器