John
John

Reputation: 197

laravel store files and access them

I store files using this code:

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

The files in this example are saved under storage/app/files/, which is fine since they are thus not publicly accessible (which they are neither supposed to). However, if I want to provide my users with a download option of these files, how will I access them? I thought of PHP reading the file and 'render' it to the user. However, I'm new to laravel and a bit lost at this point.

Any ideas?

Upvotes: 1

Views: 417

Answers (1)

apokryfos
apokryfos

Reputation: 40673

There's 3 main ways to send files as responses:

File download (uses content disposition: attachment)

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

Inline file (uses content disposition: inline)

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

There's also falling back to Symfony's streamed response for very large files.

Upvotes: 1

Related Questions