1、使用scandir函数封装递归函数
php自带函数,返回当前目录下的所有文件和文件夹。注意:会有.和..分别表示当前目录和上层目录。
function file_list($path){
$func = __FUNCTION__;
if(!file_exists($path)) {
return [];
}
$files = scandir($path);
$fileItem = [];
foreach($files as $v) {
$newPath = $path .DIRECTORY_SEPARATOR . $v;
if(is_dir($newPath) && $v != '.' && $v != '..') {
$fileItem = array_merge($fileItem, $func($newPath));
}else if(is_file($newPath)){
$fileItem[] = $newPath;
}
}
return $fileItem;
}
2、使用glob函数封装递归函数
php自带函数,功能和scandir类似,但比它更加强大灵活。
function file_list($path){
$func = __FUNCTION__;
if(!file_exists($path)) {
return [];
}
$files = glob($path.DIRECTORY_SEPARATOR.'*');
$fileItem = [];
foreach($files as $v) {
if(is_dir($v)) {
$fileItem = array_merge($fileItem, $func($v));
}else if(is_file($v)){
$fileItem[] = $v;
}
}
return $fileItem;
}
目前有 0 条评论