写一个函数,能够遍历一个文件夹下的所有文件和子文件夹。(目录操作)


listDir($dir);

function listDir($dir="./"){

    if (is_dir($dir)){
        $handle = opendir($dir);
        while (($file = readdir($handle)) !== false){
            if ($file == "." || $file == ".."){
                continue;
            }
            if (is_dir($sub_dir = realpath($dir.'/'.$file))){
                echo "文件夹下的文件:".$dir.":".$file.PHP_EOL;
                listDir($sub_dir);
            }else{
                echo "文件名为:".$file.PHP_EOL;
            }
        }
        closedir($handle);

    }else{
        echo $dir."不是目录";
    }

}


0 0
讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
帮助