PHP一行代码获取目录及子目录下所有文件名

常用方法一:读取文件目录
<?php
$dir = "file";
if (is_dir($dir)) { 
   if ($dh = opendir($dir)) {
       while (($file = readdir($dh)) !== false) {
            if ($file!="." && $file!="..") {
                echo "<a href=file/".$file.">".$file."</a><br>";
            }
       }
       closedir($dh);
   }
}
常用方法二:glob方法
<?php
$dirs = glob($tPath . '/*');
    foreach ($dirs as $path) {
        if (is_dir($path)) {
            $path = basename($path);
            $dirs_arr[] = $path;
        }
    }
}
其实一行代码就可以
<?php
$dirs = array_map('basename', glob ('*', GLOB_ONLYDIR))

Related post

微信公众号:程序员到架构师

最新文章

Return Top