递归方式遍历目录及目录下的文件

<?php

function MyReadDir($path){
    echo $path . '<br>';
    
    $source = opendir($path);

    while ($fileName = readdir($source)) {
        if ($fileName === '.' || $fileName === '..') continue;

        $dirPath = $path . '/' . $fileName;

        if (is_dir($dirPath)) {
            MyReadDir($dirPath);
        } else {
            echo $dirPath . '<br/>';
        }
    }

    closedir($source);
}

MyReadDir('./c');

//tree
/*
[root@localhost html]# tree dir/
dir/
└── c
    ├── App
    ├── User
    │   └── kevin
    │       ├── a.txt
    │       └── b
    │           └── b.txt
    └── Windows
        └── System32
            └── host.txt
*/

//输出
/*
./c
./c/User
./c/User/kevin
./c/User/kevin/a.txt
./c/User/kevin/b
./c/User/kevin/b/b.txt
./c/Windows
./c/Windows/System32
./c/Windows/System32/host.txt
./c/App
*/

输出

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容