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

2017-02-28  本文已影响0人  limgquan
<?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
*/

输出

上一篇下一篇

猜你喜欢

热点阅读