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?
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?
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.
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);
?>
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/