Discuz教程网

php显示目录下所有文件

[复制链接]
authicon 09927306 发表于 2011-1-6 12:31:54 | 显示全部楼层 |阅读模式
[php]<?
//以树型结构列出指定目录里的所有文件,如果你想知道自己某个目录里有哪些子目录和文件,可以调用这个类来查看,很方便的。

    # 演示的例子:
    $t = new TreeClimber( \"asp\" ); //新建物件,设置需要列出的目录:在此为asp目录
    echo arrayValuesToString( $t->getFileList( $t->getPath() ), \"<br/>\" );

    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;
    }
    ?>
[/php]

[php]<?
    //以下为类文件
    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;
    }
    } //至此类结束
    ?>[/php]






上一篇:在PHP中将汉字转换为拼音
下一篇:PHP生成验证码图片
authicon 卡卡猫 发表于 2011-6-17 15:59:52 | 显示全部楼层
顶你一下,好贴要顶!
authicon 皆无 发表于 2011-6-20 23:59:49 | 显示全部楼层
顶顶更健康
authicon Pianissimo 发表于 2011-6-24 13:59:52 | 显示全部楼层
这个要顶起来啊
authicon 纤陌陌 发表于 2011-6-25 05:59:44 | 显示全部楼层
支持楼主,顶一下
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-3 03:34

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表