【问题标题】:PHP download image from url without direct image namePHP从没有直接图像名称的url下载图像
【发布时间】:2021-01-02 16:05:31
【问题描述】:
{{my_salesforce_url}}/sfc/servlet.shepherd/document/download/0694U000007w5QyQAI?operationContext=S1

这个 url 包含一个名为 ine_file.jpg 的文件,当在浏览器中运行并直接下载文件时,但是当我尝试使用 php 下载并将文件保存在本地时没有工作

我尝试:

file_put_contents(public_path() . "/" . $fileName, fopen($file["url"], 'r'));

    file_put_contents(public_path() . "/" . $fileName, file_get_contents($url));

有什么想法吗? 如何从 url 获取文件并使用 php o curl 保存文件?

【问题讨论】:

  • 您是否收到错误消息? file_put_contents 和 file_get_contents 返回什么?
  • 您需要登录才能查看该图像吗?也许您的 PHP 需要获得授权才能访问您的销售人员内容?也许您应该研究一下销售人员 Rest API。

标签: php laravel curl laravel-5 php-5.6


【解决方案1】:

没有什么神奇的,你应该使用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);

【讨论】:

    【解决方案2】:

    如果您从安全服务器下载,您可以试试这个

    $options = array(
    "ssl" => array(
        "verify_peer" => false,
        "verify_peer_name" => false,
    ),
    );
    $image = file_get_contents('www.example.com/image.jpg', false, stream_context_create($options));
    $filename = 'filename.jpg';     
    $filedir = 'public/uploads/' . $filename;
    file_put_contents($filedir, $image);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-27
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-12
      相关资源
      最近更新 更多