遍历目录并保存在数组中

<?php
$path = "C:\wamp64\www\php-core";
$path = iconv("GBK", "UTF-8", $path);

if(is_dir($path)){
    print_r(loopDir($path));
}

function loopDir($path){
    $result = [];
    $dirs = scandir($path);
    foreach ($dirs as $key => $dir){
        $dir = iconv("GBK", "UTF-8", $dir);
        if($dir != '.' and $dir != '..'){
            if(is_dir($path.DIRECTORY_SEPARATOR.$dir)){
                $result[$dir] = loopDir($path.DIRECTORY_SEPARATOR.$dir);
            }else{
                $result[] = $dir;
            }
        }
    }
    return $result;
}
?>

测试结果如下:
\scandir.PNG
备注:
1、测试环境为win10,通过iconv函数保证了函数可处理中文字符

2、但当目录为中英文混合时,会出现以下错误:
Detected an illegal character in input string


文件系统

登陆发表评论