Php 卷曲图像下载

Php 卷曲图像下载,php,curl,Php,Curl,我使用的代码给了我一些问题,让我无法使用CURL获得图片 public function getRemoteFile($url, $dest, $authmode = null, $cookies = null) { $context = $this->createContext($url); $ch=$context['curlhandle']; $dl_opts = $context['opts']['dl']; $outname = $dest;

我使用的代码给了我一些问题,让我无法使用CURL获得图片

public function getRemoteFile($url, $dest, $authmode = null, $cookies = null)
{
    $context = $this->createContext($url);
    $ch=$context['curlhandle'];
    $dl_opts = $context['opts']['dl'];
    $outname = $dest;

    if ($cookies)
    {

        if (substr($url, 0, 4) == "http")
        {
            $dl_opts[CURLOPT_COOKIE] = $cookies;
        }
    }

    $fp = fopen($outname, "w");
    if ($fp == false)
    {
        $this->destroyContext($context);
       throw new Exception("Cannot write file:$outname");
    }
    $dl_opts[CURLOPT_FILE] = $fp;
    $this->setURLOptions($url, $dl_opts);
    $this->setAuthOptions($context,$dl_opts);


    // Download the file , force expect to nothing to avoid buffer save problem
    curl_setopt_array($ch, $dl_opts);
    $inf = curl_getinfo($ch);
    if (!curl_exec($ch))
    {
       if (curl_error($ch) != "")
       {
           $err = "Cannot fetch $url :" . curl_error($ch);
       }
       else
       {
           $err = "CURL Error downloading $url";
       }
       $this->destroyContext($context);
       fclose($fp);
       unlink($dest);
       throw new Exception($err);
    }
    else
    {
     $proto=$context['scheme'];
     if($proto=='http' || $proto=='https')
      {
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $ok = ($httpCode < 400);
        if(!$ok)
        {
            fclose($fp);
            @unlink($outname);
            throw new Exception('Cannot fetch URL :'.$url);
        }
      }
    }

    fclose($fp); 
    $this->destroyContext($context);

    return true;
}
所以在$dl_选项中我得到了:

Array ( [80] => 1 [42] => 0 [44] => 0 [52] => 1 [10023] => Array ( [0] => Expect: ) [19913] => [19914] => 1 [10001] => Resource id #261 [10002] => http://domain/picture/static/l0033.jpg )
信息:

  • PHP 5.3.29
  • 安全模式关闭
  • open_basedir:/usr/home/shop.domain.com/:/home/shop.domain.com/:/usr/home/services/:/usr/share/php53/

显然,允许使用CURLOPT_FOLLOWLOCATION进行301和302重定向会跟随重定向到file://url。(发现此提交到PHP源代码:)

如果允许重定向到file://URL,那么控制最初请求的URL的人可以让PHP脚本从服务器的本地磁盘获取文件。从用户处获取URL并显示内容的web服务可用于从服务器磁盘读取PHP可访问的任何文件

解决方案:

  • 上面链接中显示的提交修复了此问题。更新PHP以修复
  • 禁用CURLOPT_FOLLOWLOCATION
  • 见老问题:

curl_setopt($ch,CURLOPT_HTTPHEADER,数组(“内容类型:image/jpeg”);您的错误消息包含您的答案
设置open_basedir时无法激活
@DiegoVieira Yes。。。这就是为什么我粘贴了关于phpinfo()的信息。我不得不说这不是我的代码,我只是要解决它,但我不知道如何解决。我怎样才能取消设置open_basedir或修复它?这是针对php-7.3.0beta2的。它包含在php-7.3.0beta2中,但是如果您单击它后面的
按钮,您可以看到它也包含在许多其他版本中。毕竟,这是一个五年前的承诺。我忘了提到的是,它还必须使用足够新的cURL库版本(7.19.4或更高版本)进行编译。
Array ( [80] => 1 [42] => 0 [44] => 0 [52] => 1 [10023] => Array ( [0] => Expect: ) [19913] => [19914] => 1 [10001] => Resource id #261 [10002] => http://domain/picture/static/l0033.jpg )