时间:2021-07-01 10:21:17 帮助过:53人阅读

php获取文件夹中文件的两种方法:
传统方法:
在读取某个文件夹下的内容的时候
使用 opendir readdir结合while循环过滤 当前文件夹和父文件夹来操作的
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循环读取
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;
}推荐:PHP视频教程
以上就是php获取文件夹中文件的两种方法的详细内容,更多请关注Gxlcms其它相关文章!