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

t0berius's avatar

download file from storage (get path)

How to use the storage system to generate a file download?

Is there a function I can use to get the complete path of file or how to get this working? I would like to use the storage systems functions and not "hardcode" the mainpath into my app.

    $path = "app/storage/uploads/xy.zip";
        return response()->download($path);
0 likes
4 replies
bobbybouwmann's avatar

There is a storage_path function

$path = storage_path('uploads/xy.zip');

return response()->download($path);
t0berius's avatar

@bobbybouwmann How to define before the current storage system, like amazon, or local? I would like to use in later future for example amazon to store all my files, how can I write the code so I can easily switch from local to amazon s3?

rbayer@sbts.edu's avatar

Ran across this thread in search of an answer to a similar problem, so I thought I'd post my solution:

     $contents = Storage::disk('s3')->get($fileName);
     $tempFile = "temp.pdf";
     file_put_contents($tempFile, $contents);

      header("Content-type: application/pdf");
      header("Content-Length: " . filesize($tempFile));
      readfile($tempFile);

This would allow you to then swap out 's3' to 'local' or whatever storage you wanted to use. It also allows you to share a file that doesn't have a public url, but is only accessible from your app. It feels hacky, but works.

1 like

Please or to participate in this conversation.