PHP image download from another server not done

i want to download image from this URL

http://images0.tcdn.nl/buitenland/article26651871.ece/BINARY/q/GHEE1G3C.1

but when i try to download it with my PHP code. it only saves a DOT not the image.

Anyone can help me to download this image?

Did you save the file with a .jpg extension?

yes i tried but failed :frowning:

It is going to need a recognisable extension (jpg, jpeg) for you to use it.

i only set extension to JPG. when i save this image to my local computer and change extension to JPG it opens fine but in server its not working. im using this code. and same code works for other websites image

file_put_contents(ā€œā€¦/images/$new_image_nameā€, fopen($image_src, ā€˜r’));

are you sure the file has the extension on the server - if you have to set it locally then you have to set it on the server as well

here is my code

`<?php
$new_image_name = "tmpimage.jpg";
$image_src= "http://images0.tcdn.nl/buitenland/article26651871.ece/BINARY/q/GHEE1G3C.1";
file_put_contents("../images/$new_image_name", fopen($image_src, 'r'));
?>`

Shouldn’t you have the ā€œbā€ flag for dealing with binary data? I could imagine that image truncating as soon as it hits the first zero byte.

file_put_contents("../images/$new_image_name", fopen($image_src, 'rb'));

Strange that it works for other images if a zero is the cause, though.

Hi there jammyxml,

bearing in mind that PHP is not my forte, this worked OK for me…

<?php
   $curl = curl_init();
   $fp=$fp = fopen("somefile.jpg", "w");
   curl_setopt ($curl, CURLOPT_URL, "http://images0.tcdn.nl/buitenland/article26651871.ece/BINARY/q/GHEE1G3C.1");
   curl_setopt($curl, CURLOPT_FILE, $fp);
   curl_exec ($curl);
   curl_close ($curl);
   echo '<img src="somefile.jpg" alt="">';
?>

Where ā€œsomefile.jpgā€ is a blank file.

coothead

1 Like

sorry for late reply,
this CURL code works but it does work only on my localhost not on server.
why?

i have CURL enabled and allow_url_fopen = On on server but still not working.
it creates only blank file

At a guess, are you writing the local copy into a location that you don’t have permission to create files in?

im able to write files in directory. but with this code blank file is created and data is not written into it on server.
but on local it works fine

Is this line a typo?

   $fp=$fp = fopen("somefile.jpg", "w");

Shouldn’t it read

   $fp= fopen("somefile.jpg", "w");
1 Like

no it works same as both lines :frowning:
allow_url_fopen is disabled function on this shared server :frowning:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.