php获取文件夹中文件的两种方法
来源:尚
发布时间:2020-05-08 10:15:55
阅读量:1933

php获取文件夹中文件的两种方法:
传统方法:
在读取某个文件夹下的内容的时候
使用 opendir readdir结合while循环过滤 当前文件夹和父文件夹来操作的
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函数 可以扫描文件夹下内容 代替while循环读取
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;
}
|