php获取文件夹名字

php教程11年前 (2014-08-27)14040

php如何获取文件夹名字其实很简单,只要记住一两个函数就可以了!


$dir = "message/";  // 文件夹的名称

if (is_dir($dir)){
    if ($dh = opendir($dir)){
        while (($file = readdir($dh)) !== false){
            echo "文件名: $file <br>";
        }
        closedir($dh);
    }
}


包装成函数写法如下:
function getSubDirs($dir) {
                $subdirs = array();
                if ($dh = opendir($dir)){
                                while (($file = readdir($dh)) !== false){
                                        if($file!='..'&&$file!='.'){
                                                $subdirs[] = $file;
                                        }
                                }
                        closedir($dh);
                        return $subdirs;
                }
        }


发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。