首页 > 解决方案 > 如何从 laravel 5.5 的存储内部文件夹中获取文件?

问题描述

我需要从 storage->app->public->huetten-> 列出文件,它包含 17 个文件夹。每个文件夹都包含图像。任何帮助,将不胜感激。

public function show($id)
    {
        if(!empty($id)) {
            $directories = Storage::disk('public')->allDirectories('huetten');
            foreach ($directories as $directory) {
                //dd($directory); // Here i get huetten/folder1

                $files = Storage::files($directory);
                foreach ($files as $file) {
                    dd($file); // But here files are not listing
                }


            }
        }
    }

标签: phplaravel-5laravel-5.5

解决方案


存储使用默认的文件系统磁盘,在新的 Laravel 项目中config/filesystems.php设置为local( ie )。 storage/app/

我假设它在您的项目中设置为相同的值。

您想列出 storage/app/public 中的文件,这对应于config/filesystems.php.

在您的循环中,您可以通过获取一个用于公共磁盘的 Filesystem 实例来正确搜索目录,就像您在列出目录时所做的那样。

$files = Storage::disk('public')->files($directory);
/* [ 'storage/app/public/huetten/folder1/foo', 'storage/app/public/huetten/folder1/bar', ...] */

推荐阅读