我将所有上传内容保存在自定义的外部驱动器上。这些文件是通过自定义API存储的。
在Laravel 5.2中,我可以对本地文件进行下载:
return response()->download('path/to/file/image.jpg');
不幸的是,当我传递URL而不是路径时,Laravel抛出错误:
文件“ https://my-cdn.com/files/image.jpg ”不存在
(URL当然是虚拟的)。
有什么办法可以使用Laravel的实现下载image.jpg文件,还是可以使用普通的PHP来下载?
没有魔术,您应该使用copy()
函数下载外部图像,然后在响应中将其发送给用户:
$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);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句