0

I trying to force to download a remote image (url with https protocol), I failed to attempts do it on client (cannot use HTML5, thanks to IE8), so I'm trying to use server side (php). The only way how to do it I found thanks following answer, is using curl. Other way like readfile($file_url), always return an empty file. The problem with using curl - downloading starts after image is downloaded to server and it can take some time. Can we start loading directly from the source?

But if somebody know a way how to download an image on client side, that already on the page, it will be great!

Community
  • 1
  • 1
IgorCh
  • 2,641
  • 5
  • 22
  • 29

1 Answers1

1

You can use fopen('http://server/img.jpg'), and fread():

$handle = fopen("http://www.example.com/image.jpg", "rb");
while (!feof($handle)) {
  echo fread($handle, 8192);
}
fclose($handle);

plus headers.

Marek
  • 7,337
  • 1
  • 22
  • 33
  • $handle is empty after fopen, maybe some problems with php.ini, but "allow_url_fopen" is set to "On" state – IgorCh Nov 05 '13 at 10:27
  • one more thing that remote images url has https protocol – IgorCh Nov 05 '13 at 10:33
  • The default is not to verify peer, so certificate shouldn't be a problem. – Marek Nov 05 '13 at 11:02
  • but it was a problem, php_openssl was disabled, your solution works as well as readfile($file_url), when php_openssl is set to enabled. Thanks! – IgorCh Nov 05 '13 at 11:18