Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

splatEric's avatar

Strange issue with Storage::response

Hi, I have the following code to provide authorised access to files in the storage directory:

public function show($filename)
    {
        $available_files = collect(Storage::files('restricted_files'))
            ->map('basename');
        
        if (!$available_files->contains($filename)) {
            abort(404);
        }

        return Storage::response('restricted_files/' . $filename);
    }

Locally it works fine, but when I deploy to a server, I get 404s, even though the file definitely exists. For debug I even tried just return Storage::get('restricted_files/' . $filename); and content was returned.

Can anyone suggest anything that might help resolve this?

0 likes
1 reply
splatEric's avatar

I have got somewhere, thanks to this post discussing a similar-ish challenge.

In essence, the problem seems to lie in the fact that the filename in the URL is the filename I am loading, e.g. /files/example.pdf to retrieve example.pdf

If I remove the file extension from the URL, I am then able to return the pdf file correctly. I am not sure what configuration is going on with my server that is causing this discrepancy (I assume it's something to do with the server configuration, because I did not hit this problem in my local docker dev environment)

Anyway, I thought I should post an update for anyone who does stumble across this in the future. And of course if anyone has any ideas as to what to do resolve this, I'd appreciate any guidance available!

Please or to participate in this conversation.