dte66654 2011-02-11 11:13
浏览 41

用PHP保存远程图像

The following code retrieves an image and saves it to a local folder. A jpg file is indeed saved to local disk, with around 40KB filesize (seems correct). When I put the local path in an img tag, the file does not display.

Firebug > Inspect Element shows a size of 0 X 0 and I'm unable to view the image when saved to my desktop.

file_put_contents, file_get_contents and getimagesize don't return FAILs. $url IS a valid image. The problem is just saving it locally, the file seems to be corrupt - how come?

$url = $image->request_url; //the image generated on the remote server
    //print_r(getimagesize($url)); die;
    $img = 'thumbalizr/cache/screenshot_' . $row['id'] . '.jpg'; //path to our local cache folder + unique filename
    if( !$captured_file = file_get_contents($url) ) die('file could not be retrieved');
    elseif( !file_put_contents($img, $captured_file, FILE_APPEND) ) die('file could not be written to local disk'); //write the image to our local cache

"Are you sure the path is correct? Have you tried an absolute path?" YES

"Have you checked that the image is downloaded correctly, perhaps with another utility (e.g. ftp, diff)?" I can download the img via ftp but it does not open on my local computer either.

"What do you get if you call the URL directly in the browser?" FF just prints out the URL instead of showing the image

"Why are you using FILE_APPEND? if the target already exists, this writes to the end, which will naturally give you a corrupt image" I removed FILE_APPEND, no difference

"source and final extension are the same?" Yes I tried with jpg, jpeg and png - no difference

"First of all, example code is wrong. Can't use $capture_file in file_put_content because that variable is not defied becouso of if else if block logic." - WRONG, that code does run!

"Can you look into the image file" - no! Although the file has a realistic file size and I can download it, it's impossible to open it.

  • 写回答

1条回答 默认 最新

  • donglian1953 2011-02-21 18:44
    关注

    First off check the files you're been downloading in a text editor to see if you're getting HTML error pages instead of binary image data.

    Second, I would use curl for this as it provides better success/error information. Here's your example modified to use it.

    //path to our local cache folder + unique filename
    $img_path = 'thumbalizr/cache/screenshot_' . $row['id'] . '.jpg';
    
    $c = curl_init();
    curl_setopt($c, CURLOPT_URL, $image->request_url);
    curl_setopt($c, CURLOPT_HEADER, 0);
    curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($c, CURLOPT_AUTOREFERER, true);
    curl_setopt($c, CURLOPT_BINARYTRANSFER, true);
    curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($c, CURLOPT_FORBID_REUSE, true);
    // curl can automatically write the file out for you
    curl_setopt($c, CURLOPT_FILE, $img_path);
    
    // You can get more elaborate success/error info from 
    // curl_getinfo() http://www.php.net/manual/en/function.curl-getinfo.php
    if (!curl_exec($c)) {
        die('file could not be retrieved');
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)