【问题标题】:how to download a file from a link?如何从链接下载文件?
【发布时间】:2021-04-05 06:55:16
【问题描述】:

我有一个链接,例如“https://www.example.com/example-811.jpg” 我需要将它上传到 laravel 存储(../app/storage/public/)。 这是我可以在控制器中实现的最大值,一切都很好,但是文件以 TMP 扩展名加载,我怎样才能以所需的格式加载文件?

        $filename = 'example-811.jpg';
        $tempImage = tempnam('..\storage\app\public', $filename);
        copy('https://www.example.com/example-811.jpg', $tempImage);

        dd(response()->download($tempImage, $filename));

【问题讨论】:

  • file_get_contents可以帮你
  • 内容类型标头通常包含文件类型,从那里获取并连接到您的字符串中
  • @urfusion 那么如何将代码转换成图片呢?

标签: php laravel file download


【解决方案1】:

使用此代码下载文件:

$filename = 'example-811.jpg';
$path = public_path().'/storage/'.$filename;
return response()->download($path, $filename, ['Content-Type' => $filename]);

【讨论】:

  • 谢谢解答,相反,我需要发送文件
【解决方案2】:

或许可以做的更方便些,不过我只是从$ tempImage中去掉了行尾的4个字符

$expansion = substr($filename, -4);;
$tempImage = tempnam('..\storage\app\public', $filename);
$tempImage = substr($tempImage, 0, -4) . $expansion;

并添加了扩展名

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-27
    • 1970-01-01
    • 2018-01-19
    • 2015-06-16
    • 2017-04-05
    相关资源
    最近更新 更多