我把所有上传的东西都放在一个定制的外置硬盘上.这些文件是通过自定义API存储的.

In Laravel 5.2, I can do this for a local file to download it:

return response()->download('path/to/file/image.jpg');

不幸的是,当我传递URL而不是路径时,Laravel抛出一个错误:

文件"https://my-cdn.com/files/image.jpg"不存在

(当然,URL是一个虚拟的).

Is there any way I can download the image.jpg file using Laravel's implementation or do I do this with plain PHP instead?

推荐答案

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);

Laravel相关问答推荐

如何定制有关密码被泄露的Laravel Breeze S消息?

在Laravel中URL中添加保护ID

在 blade laravel 中以格式编号显示长数字

1 子编译中的警告(使用'stats.children:true'和'--stats-children'了解更多详细信息)

laravel Eloquent 模型更新事件未触发

为什么 laravel 模型会重复一组数据以及如何(如果可能)只有一组数据?

Laravel 4:验证前修剪输入的最佳实践

Laravel:`loadMissing` 函数的目的是什么?

PHP 中的 Http 响应代码枚举

在表单中添加一对多 - Backpack laravel

Database[] 未配置 Laravel 5

Laravel Artisan CLI 安全地停止守护进程队列工作者

Web 服务器如何处理请求?

Laravel 同步错误

如何更改默认 Laravel Auth 登录视图

Guzzle 返回流空体而不是 json 体

在 vue.js 中使用 Laravel 的Gate/Authorization

Laravel 在域和子域中共享 cookie 检测问题

Laravel X-CSRF-Token 与 POSTMAN 不匹配

Laravel or where