doutangu4671 2013-09-20 06:14
浏览 28
已采纳

图像不是使用file_get_contents下载的

I am using the below mentioned code to download image.

$image_url='http://www.abcd.com/I_298f02_4766792.jpg';

$doc_root= $_SERVER['DOCUMENT_ROOT'];
$image_upload_path = $doc_root . '/';
$image_name2 = 'img-' . time() . '.jpg';

$destination  = $image_upload_path . $image_name2;

$data = file_get_contents($image_url);
$file = fopen($destination, "w+");
fputs($file, $data);
fclose($file);

The above mentioned code is working fine in development environment to download image from any website. The code also works in live site to download images from other website except only one site.

The image seems to be downloaded but whenever i am trying to open the image using any image viewer, an error message is being displayed which is given below.

Could not load image 'img-65456465.jpg'. Error interpreting JPEG image file (Not a JPEG file: starts with 0x3c 0x21)

I checked the error log as well but found no issue. I am not understanding why its image download functionality is not working with that particular website only.

Note: 1) I checked that the original image type hosted in that website is JPEG only.

EDIT: When i am trying to view the downloaded image using browser, i found the below mentioned error.

The image 'http://www.my-live-site.com/folder/img-65456465.jpg' can not be displayed because it contains error.

As per suggestion, i am providing the original website link. Website: http://www.funnyjunk.com/ Image: any image link from this website. For example http://static.fjcdn.com/pictures/The+smarterest.+Wow+a+description+I+could+say+anything+I_298f02_4766792.jpg

Any help on this will be appreciated.

  • 写回答

4条回答 默认 最新

  • dqwn64004 2013-09-20 06:26
    关注

    What if you change the fopen() to:

    $file = fopen($destination, "wb+"); // binary writing
    

    Let me know.

    EDIT

    Here's the code that will work:

    <?php
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    ini_set('log_errors', 0);
    
    $opts = array(
      'http'=>array(
        'method'=>"GET",
        'header'=>"Accept-language: en
    " .
            "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:23.0) Gecko/20100101 Firefox/23.0
    " . 
        "Referer: http://www.funnyjunk.com
    "
      )
    );
    
    $image_url = 'http://static.fjcdn.com/pictures/The+smarterest.+Wow+a+description+I+could+say+anything+I_298f02_4766792.jpg';
    
    $doc_root= $_SERVER['DOCUMENT_ROOT'];
    $image_upload_path = $doc_root . '/';
    $image_name2 = 'img-' . time() . '.jpg';
    
    $destination  = $image_upload_path . $image_name2;
    
    $data = file_get_contents($image_url, false, stream_context_create($opts));
    //$file = fopen($destination, "w+");
    //fputs($file, $data);
    //fclose($file);
    header("Content-type: image/jpeg");
    echo $data;
    ?>
    

    stream_context_create() with the referrer to the original site did the trick.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角