遍歷目錄并保存在數(shù)組中
<?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;
}
?>
測(cè)試結(jié)果如下:
備注:
1迷雪、測(cè)試環(huán)境為win10,通過iconv函數(shù)保證了函數(shù)可處理中文字符
2葵第、但當(dāng)目錄為中英文混合時(shí)税弃,會(huì)出現(xiàn)以下錯(cuò)誤:
Detected an illegal character in input string