php获取文件夹中文件的两种方法-php教程

资源魔 13 0

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猎取文件夹中文件的两种办法的具体内容,更多请存眷资源魔其它相干文章!

标签: php php开发教程 php开发资料 php开发自学

抱歉,评论功能暂时关闭!