laravel 存储文件并访问它们

laravel store files and access them

我使用此代码存储文件:

 $files = $request->file('files');
    if($request->hasFile('files'))
    {
        foreach($files as $file)
        {
            $file->store('files'));
        }
    }

此示例中的文件保存在 storage/app/files/ 下,这很好,因为它们因此不可公开访问(它们不应该)。但是,如果我想为我的用户提供这些文件的下载选项,我将如何访问它们?我想到 PHP 读取文件并 'render' 给用户。但是,我是 laravel 的新手,此时有点迷茫。

有什么想法吗?

发送文件作为响应有 3 种主要方式:

文件下载(使用内容配置:附件)

return response()->download(storage_path("app/files/filename.ext")); //Where filename.ext is the file you want to send

内联文件(使用内容配置:内联)

return response()->file(storage_path("app/files/filename.ext"));

对于非常大的文件,也会回退到 Symfony's streamed response