0

i used this code to save image file. but its returning an empty file .

function save_from_url($inPath,$outPath) {

$in=    fopen($inPath, "rb");
$out=   fopen($outPath, "wb");
while ($chunk = fread($in,8192))
{
    fwrite($out, $chunk, 8192);
}
fclose($in);
fclose($out);

}

save_from_url('url/eg.php','flw.png');

  • possible duplicate of [Saving image from PHP URL using PHP](http://stackoverflow.com/questions/724391/saving-image-from-php-url-using-php) – evuez Nov 26 '13 at 09:32

2 Answers2

2

Does your server have allow_url_fopen turned on?

If it does, using file_get_contents and file_put_contents is quite simple:

$in = file_get_contents('http://example.com/image.php');
file_put_contents('file.png', $in);

If not, you will have to use cURL:

$in = curl_init('http://example.com/image.php');
$out = fopen('file.png', 'wb');
curl_setopt($in, CURLOPT_FILE, $out);
curl_setopt($in, CURLOPT_HEADER, 0);
curl_exec($in);
curl_close($in);
fclose($out);
evuez
  • 3,257
  • 4
  • 29
  • 44
0

Or simply copy it from a remote URL:

copy('http://www.google.com/images/srpr/logo11w.png', '/tmp/google.png');

Chris Wheeler
  • 1,623
  • 1
  • 11
  • 18