使用PHP从URL保存图像(以0KB文件大小保存)

What I'm trying to do:

file_get_contents() an online image, then save it to a directory on my server, with a new filename. The save path / directory is writable. CHMOD permissions: 777


What I've tried:

  • file_put_contents($filepath, file_get_contents($image_url)); // SAVES 0KB IMAGE

  • copy($image_url,$filepath); // SAVES 0KB IMAGE

  • cURL // SAVES 0KB IMAGE

So I'm already getting confused why none of this is working, when it should be...


Here are the relevant values inside my PHPinfo:

allow_url_fopen = On  
allow_url_include = On  
open_basedir = no value  

post_max_size = 8M  
upload_max_filesize = 50M  
upload_tmp_dir = no value  

max_execution_time = 18000  
max_file_uploads = 20  
max_input_nesting_level = 64  
max_input_time = 60  
max_input_vars = 1000  
memory_limit = 256M

And here is my full PHP code (I'm now using a function to save the image, because I'm just about desperate enough to try anything):

function save_image($inPath,$outPath)
{ // Download images from remote server
    $in=    fopen($inPath, "rb");
    $out=   fopen($outPath, "wb");
    while ($chunk = fread($in,8388608))
    {
        fwrite($out, $chunk, 8388608);
    }
    fclose($in);
    fclose($out);
}

// THE ORIGINAL IMAGE TO GET
$image_url = 'http://m1.i.pbase.com/u38/antidote3/upload/31848351.Cute.jpg';

// THE IMAGE EXTENSION
$image_type = substr(strrchr($image_url,"."),1);

// THE NEW FILE NAME
$filename   = 'product5_'. md5($image_url . $sku).'.'.$image_type;

// THE SAVE PATH
// OUTPUT: /var/www/site/media/catalog/product/FILE_NAME.FILE_TYPE
$filepath   = Mage::getBaseDir('media') . DS . 'catalog'. DS . 'product'. DS . $filename;

// SAVE THIS BIATCH!
save_image($image_url, $filepath);

// OTHER SAVE METHODS
// file_put_contents($filepath, file_get_contents($image_url));
// copy($image_url,$filepath);

Running strlen(file_get_contents($image_url)) on the image returns a big fat 0.
But shouldn't it work with the following enabled on my server?

allow_url_fopen = On  
allow_url_include = On

Apparently, the problem doesn't lie on the writing, but file_get_contents($image_url) doesn't have anything inside it.

Any ideas why? Thanks!


Other Information:

  • Platform: Magento
  • Language: PHP
  • PHP Version: 5.4.6

The answer is: a firewall rule on the server was causing it. Thanks everyone for the help!

Maybe you should try something like this (using feof and fread the right way!) - I also changed how you are grabbing the extension:

<?php
function save_image($inPath, $outPath) {
  $in  = fopen($inPath,  "rb");
  $out = fopen($outPath, "wb");

  while (!feof($in)) {
    $read = fread($in, 8192);
    fwrite($out, $read);
  }

  fclose($in);
  fclose($out);
}

// THE ORIGINAL IMAGE TO GET
$image_url = 'http://m1.i.pbase.com/u38/antidote3/upload/31848351.Cute.jpg';

// THE IMAGE EXTENSION
$image_type = end(explode(".", $image_url));

// THE NEW FILE NAME
$filename   = 'product5_'. md5($image_url . $sku).'.'.$image_type;

// THE SAVE PATH
// OUTPUT: /var/www/site/media/catalog/product/FILE_NAME.FILE_TYPE
$filepath   = Mage::getBaseDir('media') . DS . 'catalog'. DS . 'product'. DS . $filename;

// SAVE THIS BIATCH!
save_image($image_url, $filepath);
?>

I not sure will it help. Can you try this way?

function save_image($inPath,$outPath) {
  $inbuf = file_get_contents($inPath);
  $fp = fopen($outPath,'w');
  fwrite($fp,$inbuf);
  fclose($fp);
}

note: it was a typo, $buf intead of $inbuf.