【问题标题】:Image is not downloading using file_get_contents未使用 file_get_contents 下载图像
【发布时间】:2013-09-25 10:38:25
【问题描述】:

我正在使用下面提到的代码来下载图像。

$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);

上述代码在开发环境中运行良好,可以从任何网站下载图像。该代码也适用于实时站点,以从除一个站点之外的其他站点下载图像。

图像似乎已下载,但每当我尝试使用任何图像查看器打开图像时,都会显示一条错误消息,如下所示。

无法加载图像“img-65456465.jpg”。 解释 JPEG 图像文件时出错(不是 JPEG 文件:以 0x3c 0x21 开头)

我也检查了错误日志,但没有发现任何问题。 我不明白为什么它的图像下载功能仅适用于该特定网站。

注意: 1) 我检查了该网站中托管的原始图像类型仅为 JPEG。

编辑: 当我尝试使用浏览器查看下载的图像时,我发现了下面提到的错误。

图片“http://www.my-live-site.com/folder/img-65456465.jpg”包含错误,无法显示。

根据建议,我提供原始网站链接。 网站: http://www.funnyjunk.com/ 图片:来自本网站的任何图片链接。例如http://static.fjcdn.com/pictures/The+smarterest.+Wow+a+description+I+could+say+anything+I_298f02_4766792.jpg

我们将不胜感激。

【问题讨论】:

    标签: php file download


    【解决方案1】:

    如果将 fopen() 更改为:

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

    告诉我。

    编辑

    下面是有效的代码:

    <?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\r\n" .
            "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:23.0) Gecko/20100101 Firefox/23.0\r\n" . 
        "Referer: http://www.funnyjunk.com\r\n"
      )
    );
    
    $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() 加上指向原始网站的引荐来源就可以了。

    【讨论】:

    • 我尝试了您的解决方案,但没有奏效。遇到同样的问题。
    • 您使用的是哪个操作系统?
    • 请注明网址。里面的图片应该没有问题。
    • 我将下载的图像转移到我的开发工作区。我在开发工作区中使用 UBUNTU?但是,它依赖于操作系统吗?我也尝试使用浏览器查看下载的图像,发现错误(在上面的描述中提到)。
    • 图像查看与操作系统无关,但文件写入可以。在您的情况下,图像似乎受到站点的某种保护。如果你能指出我的网站,那将是一个帮助。
    【解决方案2】:

    你的代码很好,我已经用一个新的 url 测试过,试试这个代码

    <?php
    $image_url='http://blog.svnlabs.com/wp-content/uploads/2012/06/PHP-DOWNLOAD-FILES-BY-URL.png';
    
    $doc_root= $_SERVER['DOCUMENT_ROOT'];
    echo $doc_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);
    echo $file;
    ?>
    

    【讨论】:

    • 是的,我的代码可以正常工作,可以从几乎所有网站下载图片,但只有一个网站除外。每当我尝试打开下载的图像时,我都会注意到上述错误消息。我可以在这里提及网站名称吗?我可以提及网站链接吗?
    【解决方案3】:

    您提供的链接在下载时不是图片。当您在浏览器中打开图像 URL 时,您将被重定向到包含该图像的 HTML 页面。当我在这张图片上运行文件命令时确认了这一点,它告诉我这是一个 HTML 文本文档。

    piNAS:~#: file The+smarterest.+Wow+a+description+I+could+say+anything+I_298f02_4766792.jpg
    The+smarterest.+Wow+a+description+I+could+say+anything+I_298f02_4766792.jpg: HTML document text
    

    可能是设置了图片不能直接链接,然后重定向到HTML页面。

    【讨论】:

      【解决方案4】:

      嘿,打开你的网址

      http://static.fjcdn.com/pictures/The+smarterest.+Wow+a+description+I+could+say+anything+I_298f02_4766792.jpg

      并刷新此页面。 您将重定向到其他页面我认为这是问题图像已被保护。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-05-16
        • 2018-07-27
        • 2012-09-16
        • 2012-08-18
        • 2013-06-23
        • 2014-07-29
        相关资源
        最近更新 更多