php獲取文件夾中文件的兩種方法
來源:尚
發(fā)布時(shí)間:2020-05-08 10:15:55
閱讀量:1177

php獲取文件夾中文件的兩種方法:
傳統(tǒng)方法:
在讀取某個(gè)文件夾下的內(nèi)容的時(shí)候
使用 opendir readdir結(jié)合while循環(huán)過濾 當(dāng)前文件夾和父文件夾來操作的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | function readFolderFiles($path)
{
$list = [];
$resource = opendir($path);
while ($file = readdir($resource))
{
if ($file != ".." && $file != ".")
{
if (is_dir($path . "/" . $file))
{
$list[$file] = readFolderFiles($path . "/" . $file);
}
else
{
$list[] = $file;
}
}
}
closedir($resource);
return $list ? $list : [];
}
|
方法二
使用 scandir函數(shù) 可以掃描文件夾下內(nèi)容 代替while循環(huán)讀取
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | function scandirFolder($path)
{
$list = [];
$temp_list = scandir($path);
foreach ($temp_list as $file)
{
if ($file != ".." && $file != ".")
{
if (is_dir($path . "/" . $file))
{
$list[$file] = scandirFolder($path . "/" . $file);
}
else
{
$list[] = $file;
}
}
}
return $list;
}
|