I'm keeping all uploads on a custom, external drive. The files are being stored via custom API.
In Laravel 5.2, I can do this for a local file to download it:
return response()->download('path/to/file/image.jpg');
Unfortunately, when I pass a URL instead of a path, Laravel throws an error:
The file "https://my-cdn.com/files/image.jpg" does not exist
(the URL is a dummy of course).
Is there any way I can download the image.jpg file using Laravel's implementation or do I do this with plain PHP instead?
Downloading files in Laravel is even more simple than uploading. You can pass download() method with file path to download file. Same way, if you want to download file from the public folder, you can use download() method from Response class.
If you have file object from request then you can simply get by laravel function. $extension = $request->file->extension(); dd($extension); If you have file object from request then you can simply get by laravel function.
There's no magic, you should download external image using copy()
function, then send it to user in the response:
$filename = 'temp-image.jpg'; $tempImage = tempnam(sys_get_temp_dir(), $filename); copy('https://my-cdn.com/files/image.jpg', $tempImage); return response()->download($tempImage, $filename);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With