获取指定文件夹下的所有文件,可以通过遍历去获取。
function open_dir($path) { if (!is_dir($path) || empty($path)) die("该" . $path . "不是目录"); $path = $path . '/'; $fileList = []; $dirFile = scandir($path); if (!empty($dirFile) && is_array($dirFile)) { foreach ($dirFile as $file) { if ($file != '.' && $file != '..') { $fullPath = $path . $file; if (is_dir($fullPath)) { // 子文件夹,进行递归 $fileList[$file] = open_dir($fullPath); } else { //根目录下的文件 $fileList[] = $file; } } } } return $fileList; } $list = open_dir("D:/phpstudy_pro/WWW/suofang"); echo '<pre>'; print_r($list); echo '</pre>';
运行效果: