Mark
Mark

Reputation: 97

Amazon S3 Download Image File by PUBLIC URL

I have uploaded my images using Amazon S3 Services at this location "https://s3.amazonaws.com/qbprod/" After uploading i get response like

https://s3.amazonaws.com/qbprod/70dcdd564f5d4b15b32b975be15e4a1200

I try to get image by below ways but not getting success.

(1)................
    $xman = explode("/",$ImageSTR);
    //$saveto = end($xman).'.jpg';
    $saveto = end($xman);
    $fl = file_get_contents($ImageSTR);
    file_put_contents('abcd.jpg',$fl);

(2)................
    grab_image($ImageSTR,$saveto);  

    function grab_image($url,$saveto){
        $ch = curl_init ($url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
        $raw=curl_exec($ch);
        curl_close ($ch);
        if(file_exists($saveto)){
            unlink($saveto);
        }
        $fp = fopen($saveto,'x');
        fwrite($fp, $raw);
        fclose($fp);
    }

Thank You In Advance

Upvotes: 1

Views: 1910

Answers (1)

Viet Nguyen
Viet Nguyen

Reputation: 2373

U can see this is https and you need using CURLOPT_SSL_VERIFYPEER like this

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

And this is final function is

function grab_image($url,$saveto){
    $ch = curl_init ($url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    $raw=curl_exec($ch);
    curl_close ($ch);
    if(file_exists($saveto)){
        unlink($saveto);
    }
    $fp = fopen($saveto,'x');
    fwrite($fp, $raw);
    fclose($fp);
}

You can read here for more CURL http://php.net/manual/en/function.curl-setopt.php

Hope this help!

Upvotes: 3

Related Questions