【问题标题】:Cant able to copy image from an external url to my server无法将图像从外部 url 复制到我的服务器
【发布时间】:2015-02-19 08:49:46
【问题描述】:

我正在尝试将存储在 mysql 数据中的一堆图像文件链接从一台服务器复制到另一台服务器,这有点像在后台运行的 cron 作业,它检查外部链接并将其复制到服务器。手动查看时显示的链接很少,但是当我尝试通过 php 代码复制文件时没有任何反应。它只是输出空字符串。

以下是我尝试复制到我的服务器的几个链接。

  1. http://crm.propspace.com/watermark?c_id=1646&l_id=2300187&aid=1444765&id=14181282100524636&image=09_12_2014-16_53_34-1646-b15d08432d253ac710dc7bea47bf67c9.png
  2. http://crm.propspace.com/watermark?c_id=1646&l_id=2257894&aid=1444765&id=14169037278702926&image=25_11_2014-12_29_24-1646-67974878d8b7abd86d86c93527e15ebd.jpg
  3. http://crm.propspace.com/watermark?c_id=1646&l_id=2257894&aid=1444765&id=14169037278702926&image=25_11_2014-12_29_31-1646-0cf46efe5b2f89214001a2e050353736.jpg
  4. http://crm.propspace.com/watermark?c_id=1646&l_id=2282465&aid=1444765&id=14176053840492751&image=03_12_2014-16_36_29-1646-46cd5f89122e6899f8185e20bd942dd7.png

链接打开并显示图像,但是当我尝试通过 php 代码复制时,我得到的只是空数据。我尝试了所有类型的方法来复制图像,例如

1. copy()
2. fopen()
3. file_get_contents()
4. curl_init()

但一切都以空字符串或数据结束。谁能帮我解决这个问题。

用于检索图像文件的方法

$img_link       =   "image from link";
$upload_link    =   "image to save link";


Method 1
------------------------------------------------------------------------------
$img_cont       =   file_get_contents($img_link);
file_put_contents($upload_link, $img_cont);



Method 2 - (This method gives me an image with 0 bytes)
------------------------------------------------------------------------------
copy($img_link, $upload_link);


Method 3
------------------------------------------------------------------------------
$img_file   =   fopen($img_link, 'r');
$img_data   =   '';
while($chunk = fread($img_file, 8192))
$img_data   .=  $chunk;
fclose($img_file);


Method 4
------------------------------------------------------------------------------
$img_stream =   fopen($img_link, 'r');
$img_data   =   stream_get_contents($img_stream);
fclose($img_stream);


Method 5
------------------------------------------------------------------------------
$referer    =   "http://www.propspace.com/"
$headers    =   array(
    'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg, image/png',
    'Connection: Keep-Alive',
    'Content-type: application/x-www-form-urlencoded;charset=UTF-8'
);

// Initialize cURL Connection
$conn           =   curl_init();

// Set cURL Options
curl_setopt($conn, CURLOPT_URL, $img_link);

// Add Headers
if(is_array($headers) && count($headers) > 0){
    curl_setopt($conn, CURLOPT_HTTPHEADER, $headers);
}

// Add References
if($reference != "")
curl_setopt($conn, CURLOPT_REFERER, $reference);

// Other Options
curl_setopt($conn, CURLOPT_RETURNTRANSFER, true);
curl_setopt($conn, CURLOPT_BINARYTRANSFER, true);
curl_setopt($conn, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($conn, CURLOPT_FAILONERROR, true);
curl_setopt($conn, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($conn, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($conn, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($conn, CURLOPT_HEADER, 0);
curl_setopt($conn, CURLOPT_FOLLOWLOCATION, 1);

// Execute Request
$img_cont       =   curl_exec($conn);

谢谢。

更新:刚刚发现了一个使图像显示为 0 大小的东西。它只能从迪拜或阿联酋网络访问。我怎样才能让它工作呢?

【问题讨论】:

  • 你可以添加你的代码吗?
  • 请检查一下我已经提到了我使用的方法...
  • 如果您在将内容放在服务器上时遇到问题,则可能是权限问题,这意味着您必须为您尝试在服务器上保存图像的文件夹设置 777。
  • 不,这不是问题...问题在于从我在上面的帖子中提到的某些链接中读取图像文件...
  • 我无法在浏览器中访问它们。

标签: php mysql curl fopen file-get-contents


【解决方案1】:

您尝试下载只有正确登录才能访问的通用图像。您必须使用您的服务器“登录”,然后您才能执行此操作。我用cURL 做过一次。

【讨论】:

  • 好吧,这些图片无需登录即可访问...即使我尝试使用 cURL,但我得到的是空字符串/数据...
  • 我们无法访问您的网址。它们是 0 尺寸的图像。此外,您似乎没有在 curl 中使用 cookie,也没有登录请求。
  • 刚刚发现了一个使图像显示为 0 大小的东西。它只能从迪拜或阿联酋网络访问。如果我尝试通过 vpn 连接到其他服务器,即使我得到相同的 0 尺寸图像...有没有办法克服这个问题?
  • 您必须使用代理,但您必须找到一个可靠的代理。而且我不知道如何通过 php 使用代理。
【解决方案2】:

这可能有很多原因。您收到任何警告/错误吗? 也许你的 php.ini 中的“allow_url_fopen”设置为“off”

能否提供一些示例代码?

【讨论】:

  • 我确实检查了“allow_url_fopen”并且它处于“开启”状态......并且没有警告或错误,这就是奇怪的部分......:D
【解决方案3】:

我用这个方法复制外部图片

public function update_pics($url)
    {

            $image_array=explode('/',$url);
            if($image_array[0]!='http:')
            {
                $url='http:'.$url;
            }
              $image_name=strtolower($image_array[count($image_array)-1]);
            $supported_image = array(
                'gif',
                'jpg',
                'jpeg',
                'png'
            );
            $ext = strtolower(pathinfo($image_name, PATHINFO_EXTENSION));
            if (!in_array($ext, $supported_image)) {
                $image_name=$image_name.'.jpg';
            } 
            if(copy($url, './uploads/'.$image_name))
            {

                $target = 'uploads/';
                $config['image_library'] = 'gd2';
                $config['source_image'] = $target.$image_name;
                $config['create_thumb'] = FALSE;
                $config['maintain_ratio'] = FALSE;
                $config['width'] = $width;
                $config['height'] = $height;    
                $config['new_image'] = $target.$image_name;     
                $this->load->library('image_lib', $config);
                $this->image_lib->initialize($config); 
                $this->image_lib->resize();
                $this->image_lib->clear();
            }       
    }

【讨论】:

    猜你喜欢
    • 2011-09-12
    • 1970-01-01
    • 2016-02-28
    • 2013-05-11
    • 2013-04-28
    • 1970-01-01
    • 2014-12-09
    • 1970-01-01
    • 2019-10-06
    相关资源
    最近更新 更多