Amazon S3 Download Image File by PUBLIC URL - php

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

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!

Related

uploading ftp file in php

does anyone here know how to upload file to shutterstock ftp using php code?
$upURL = "ftp.shutterstock.com/";
echo $upURL;
//die();
$fp = fopen($localfile, "r");
// echo $fp;
curl_setopt($ch, CURLOPT_URL, "ftp://".$upURL.$remotefile);
curl_setopt($ch, CURLOPT_USERPWD, $user.":".$pass);
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, (int)$img->image_size);
curl_exec ($ch);
$error_no = curl_errno($ch);
curl_close ($ch);

How to upload file by PHP cURL using ftp connection?

I am using this script to upload myfile by curl function and ftp connection.
In local it works fine but in my server file is appeared uploaded but it have zero file size.
what is wrong? Thank You.
$ch = curl_init();
$localfile = (dirname(__FILE__).'/asset/myfile.zip');
$fp = fopen($localfile, 'r');
curl_setopt($ch, CURLOPT_URL, "ftp://$user_name:$user_pass#$server/".'myfile.zip');
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
curl_exec ($ch);
curl_close ($ch);
and also how can I upload multiple files in this script like:
$localfile1 = (dirname(__FILE__).'/asset/myfile1.zip');
$localfile2 = (dirname(__FILE__).'/asset/myfile2.zip');
$localfile3 = (dirname(__FILE__).'/asset/myfile3.zip');
This source may helps your problem.
http://www.web-development-blog.com/archives/tutorial-ftp-upload-via-curl/
To upload the file in curl you can use the curl_file_create.Try the below one:
$localfile = (dirname(__FILE__).'/asset/myfile.zip');
$curl_file = curl_file_create($localfile,'zip');
$params = ['file' => $curl_file];
$ch = curl_init();
$localfile = (dirname(__FILE__).'/asset/myfile.zip');
$fp = fopen($localfile, 'r');
curl_setopt($ch, CURLOPT_URL, "ftp://$user_name:$user_pass#$server/".'myfile.zip');
curl_setopt($ch, CURLOPT_UPLOAD, 1);
//curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
curl_exec ($ch);
curl_close ($ch);

how to save remote image to a file using php curl?

i am trying to save a remote image to a file using php curl but the file never get saved! could any one help me how to troublshoot this problem ? the image file never get created and but echo $returned_content has the data!
<?
$returned_content = get_data('http://somesite.com/43534545345dfsdfdsfdsfds.jpg');
echo $returned_content;
$fp = fopen('43534545345dfsdfdsfdsfds.jpg', 'w');
fwrite($fp, $returned_content);
fclose($fp);
/* gets the data from a URL */
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
?>
<?php
$ch = curl_init('http://www.example/2012/09/flower.jpg');
$fp = fopen('/localProject/imagesFolder/newname.jpg', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
?>
Try this
<?php
function getImagen($url, $rename, $ch)
{
$ch = curl_init($url);
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("$rename.jpg",'w');
fwrite($fp, $rawdata);
fclose($fp);
}
$ch = curl_init();
$image ="http://sampleurl.com/imagen.jpg";
getImagen ($image, "imagen", $ch);
?>
Works 100% :)

Grabbing image with cURL php

Trying to save image to my server using cURL. The image appears to download. It shows the correct bytes but when i link image does not work. I then DL to see and nope its a blank image.
here is my code... whats the issue with it?
$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("$rename.jpg",'w');
fwrite($fp, $rawdata);
fclose($fp);
I test your script it work fine for me, just remove the useless double quote and dot for $image.
<?
$image ="http://cdn.sstatic.net/stackoverflow/img/sprites.png?v=5";
$rename="123";
$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("$rename.jpg",'w');
fwrite($fp, $rawdata);
fclose($fp);
?>

Downloading an Image From an External URL With cURL

How do I download an external image off a url with cURL?
See here:
http://www.edmondscommerce.co.uk/php/php-save-images-using-curl/
function save_image($img,$fullpath){
$ch = curl_init ($img);
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);
if(file_exists($fullpath)){
unlink($fullpath);
}
$fp = fopen($fullpath,'x');
fwrite($fp, $rawdata);
fclose($fp);
}
Other articles/sources:
http://forums.digitalpoint.com/showthread.php?t=371632
http://www.bitrepository.com/download-image.html
http://php.bigresource.com/Track/php-Jjg3DsKY/
from php.net - also reads into a string.
<?php
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "example.com");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
?>

Categories