PHP遍历文件夹及子文件夹所有文件

<?php
function traverseDir($path = ".") {
    $currentDir = opendir ( $path ); // opendir()返回一个目录句柄,失败返回false
    while ( ($file = readdir ( $currentDir )) !== false ) { // readdir()打开目录句柄中的一个条目
        $subDir = $path . DIRECTORY_SEPARATOR . $file; // 构建子目录路径
        if ($file == "." || $file == "..") {
            continue;
        } elseif (is_dir ( $subDir )) { // 如果是目录直接递归
            echo "Directory" . $file . ":<br/>";
            traverseDir ( $subDir );
        } else {
            echo "File in the dir $path : $file <br/>"; // 如果是文件直接输出
        }
    }
}
traverseDir ( "test" );
?>

 

posted @ 2014-06-09 16:00  eterwei  阅读(212)  评论(0编辑  收藏  举报