3

I have an image file. Example: http://images5.fanpop.com/image/photos/31100000/random-random-31108109-500-502.jpg

I want to save the image to a directory called images-folder in my host. What would be the best way to do this using PHP?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 1
    http://stackoverflow.com/questions/724391/saving-image-from-php-url-using-php – CharliePrynn Feb 27 '13 at 16:36
  • http://www.edmondscommerce.co.uk/php/php-save-images-using-curl/ "`imagecreatefromjpeg($img);` where $img is a url for an external image" The site itself deals with the 'what if your host disabled this' – AmazingDreams Feb 27 '13 at 16:36
  • 2
    Thank you guy's you all helped allot and I have exactly what I need now because of the help, I am relay grateful :) –  Feb 27 '13 at 17:04

3 Answers3

14

Yes, it is very simple. Here is a little cURL script to do just that:

$image_link = "http://images5.fanpop.com/image/photos/31100000/random-random-31108109-500-502.jpg";//Direct link to image
$split_image = pathinfo($image_link);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL , $image_link);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.A.B.C Safari/525.13");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$response= curl_exec ($ch);
curl_close($ch);
$file_name = "images-folder/".$split_image['filename'].".".$split_image['extension'];
$file = fopen($file_name , 'w') or die("X_x");
fwrite($file, $response);
fclose($file);

This should do what you want. It will save the image to the directory and then name the image as random-random-31108109-500-502<-filename .jpg<-extension.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
8

Even simpler without cURL:

<?php
    $link= "http://images5.fanpop.com/image/photos/31100000/random-random-31108109-500-502.jpg";
    $destdir = 'images-folder/';
    $img=file_get_contents($link);
    file_put_contents($destdir.substr($link, strrpos($link,'/')), $img);
?>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ghigo
  • 2,312
  • 1
  • 18
  • 19
1

Here is another, somewhat easier to follow, example, straight from the site I commented

$remote_img = 'http://www.somwhere.com/images/image.jpg';
$img = imagecreatefromjpeg($remote_img);
$path = 'images/';
imagejpeg($img, $path);

http://www.edmondscommerce.co.uk/php/php-save-images-using-curl/

AmazingDreams
  • 3,136
  • 2
  • 22
  • 32
  • Explain the downvote, please. – AmazingDreams Feb 27 '13 at 16:45
  • 2
    I don't see why you got down voted either your solution work's good , I will up vote you because your answer is genuine. –  Feb 27 '13 at 17:10
  • 3
    You don't need to read the file as an image to download the image. You're allocating all of that memory and creating an editable image buffer with all of the overhead associated with that JUST to save the image. That will not scale. Also you're introducing a security risk by asking PHP to interpret a remote file as a JPEG. libjpeg exploits aren't unknown. These are the reasons I downvoted. – hsanders Mar 20 '13 at 15:46