0

I am trying to get an image by url, and save it my web server, doesn't matter where, it could be next to this php file or maybe in an /images/ folder.

This is what I have so far, and it's not working.

$url = 'https://path-to-my-image/image.jpg';
$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);
$base = base64_encode($raw);

$image = imagecreatefromstring($raw);


file_put_contents( '/images/',  $base );
John Doe
  • 571
  • 1
  • 9
  • 26

2 Answers2

0

Try it this way:

$img = file_get_contents('http://www.dan-dare.org/FreeFun/Images/CartoonsMoviesTV/BugsLifeWallpaper1024.jpg');
file_put_contents('img.jpg', $img);
Reza Saadati
  • 5,018
  • 4
  • 27
  • 64
0

from PHP - Copy image to my server direct from URL

try this

1

//Get the file
$content = file_get_contents("http://www.google.co.in/intl/en_com/images/srpr/logo1w.png");
//Store in the filesystem.
$fp = fopen("/location/to/save/image.jpg", "w");
fwrite($fp, $content);
fclose($fp);

or try this

2

function getimg($url) {         
    $headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg';              
    $headers[] = 'Connection: Keep-Alive';         
    $headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';         
    $user_agent = 'php';         
    $process = curl_init($url);         
    curl_setopt($process, CURLOPT_HTTPHEADER, $headers);         
    curl_setopt($process, CURLOPT_HEADER, 0);         
    curl_setopt($process, CURLOPT_USERAGENT, $user_agent); //check here         
    curl_setopt($process, CURLOPT_TIMEOUT, 30);         
    curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);         
    curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);         
    $return = curl_exec($process);         
    curl_close($process);         
    return $return;     
} 

$imgurl = 'http://www.foodtest.ru/images/big_img/sausage_3.jpg'; 
$imagename= basename($imgurl);
if(file_exists('./tmp/'.$imagename)){continue;} 
$image = getimg($imgurl); 
file_put_contents('tmp/'.$imagename,$image);    

if not give a try to this one.

3

$url="http://www.google.co.in/intl/en_com/images/srpr/logo1w.png";
$contents=file_get_contents($url);
$save_path="/path/to/the/dir/and/image.jpg";
file_put_contents($save_path,$contents);   
Community
  • 1
  • 1
Raheel Khan
  • 464
  • 10
  • 12