search
Homephp教程php手册树型结构列出指定目录里所有文件的PHP类

树型结构列出指定目录里所有文件的PHP类

Jun 13, 2016 pm 12:39 PM
phpbylistdesignationdocumentofTable of contentskindstructure


//以树型结构列出指定目录里的所有文件,如果你想知道自己某个目录里有哪些子目录和文件,可以调用这个类来查看,很方便的。

    # 演示的例子:
    $t = new TreeClimber( "asp" ); //新建物件,设置需要列出的目录:在此为asp目录
    echo arrayValuesToString( $t->getFileList( $t->getPath() ), "
\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         $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( "注意: TreeClimber.getFileList( $dirname ): 不能打开 $dirname!" );
    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;
    }
    } //至此类结束
    ?>

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools