CURL图片下载

时间:2018-08-21 09:48:15

标签: 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;

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

我得到以下输出:

  

警告:curl_setopt_array():启用safe_mode或在第290行的/usr/home/shop.domain.com/web/productimport/inc/remotefilegetter.php中设置了open_basedir时,无法激活CURLOPT_FOLLOWLOCATION。

我还尝试查看 $ dl_opts 会发生什么,因为在我打电话时错误开始出现:

curl_setopt_array($ch, $dl_opts);

所以在 $ dl_opts 里面,我得到了:

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 /

1 个答案:

答案 0 :(得分:0)

显然,使用CURLOPT_FOLLOWLOCATION允许301和302重定向将跟随重定向到file:// URL。 (找到对PHP源代码的提交:https://github.com/php/php-src/commit/fba290c061027c24e4c8effdba37addd3430c3d4

如果允许重定向到file:// URL,则控制最初请求的URL的任何人都可以使您的PHP脚本从服务器的本地磁盘中获取文件。然后,可以使用从用户获取URL并显示内容的Web服务,从PHP可以访问的服务器磁盘中读取任何文件。

解决方案: