image download by php curl or file_get_content

5.2k views Asked by At

I am trying to download image from here

the above url by using both php curl and file_get_content. But when i am using php curl the image is downloaded but it have no content, when i am using file_get_content the following error occurs,

Warning: file_get_contents(http://s3.amazonaws.com/test_bucket/test_12_12_2013_10_51_00.jpg) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 505 HTTP Version Not Supported in /home/webdev/public_html/example/photos/download.php on line 11

1

There are 1 answers

0
Shankar Narayana Damodaran On BEST ANSWER

First of all the link you provided is not an image. It outputs..

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<Error>
<Code>AllAccessDisabled</Code>
<Message>All access to this object has been disabled</Message>
<RequestId>35DFBD44D75B45C4</RequestId>
<HostId>
n1TT+gOUuMJ3RydGND/s3QL73JJEadz/2uQutK4NNUDbEaGX6EI0Y8a/6eDHLR6H
</HostId>
</Error>

Tested & Works !

Below code reads an image from an URL, saves it and then opens/renders on browser.

<?php
$image ="http://blog.stackoverflow.com/wp-content/uploads/stackoverflow-logo-300.png";
$ch = curl_init($image);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata=curl_exec ($ch);
curl_close ($ch);
$fp = fopen("test.jpg",'w');
fwrite($fp, $rawdata);
fclose($fp);
header ("Content-Type: image/png");
readfile("test.jpg");

OUTPUT :

enter image description here

Or

If you want just to render it.. Do like this.

<?php
readfile("http://blog.stackoverflow.com/wp-content/uploads/stackoverflow-logo-300.png");
header ("Content-Type: image/png");