Tuesday, May 23, 2017

Laravel: How to create a download response for an external file?

I know there have already been questions about downloading files with Laravel, but all I found deal with local files.

So, normally, I would do something like this:

$path = storage_path().'/my-file.pdf';
$name = 'new-name.pdf';
$headers = '....';

return response()->download($path, $name, $headers);

But what do I do, when the file I need to download is external, laying under www.my-storage.net/files/123/my-file.pdf ?

I could use the copy() function, or a combination of file_get_contents() and Laravel's Storage::put(), but I don't want to store the file locally (and temporarily) every time a download is being processed. Instead, I want to download the external file directly via user's browser.

(1) How do I prepare such Laravel download response?

(2) How do I make sure it stays efficient (so, e.g. no memory leaks)?



via lesssugar

Advertisement