70

Possible Duplicate:
save image from php url using php

I want to have PHP code for following.

Suppose I have one image URL, for example, http://www.google.co.in/intl/en_com/images/srpr/logo1w.png

If I run one script, this image will be copied and put on my server within folder having 777 rights.

Is it possible? If yea, can you please give direction for same?

Thanks,

Ian

Community
  • 1
  • 1
Ian
  • 701
  • 1
  • 5
  • 3
  • ...and http://stackoverflow.com/questions/909374/copy-image-from-remote-server-over-http – dotty Jun 10 '11 at 13:27

5 Answers5

156

Two ways, if you're using PHP5 (or higher)

copy('http://www.google.co.in/intl/en_com/images/srpr/logo1w.png', '/tmp/file.png');

If not, use file_get_contents

//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.png", "w");
fwrite($fp, $content);
fclose($fp);

From this SO post

ZioBit
  • 905
  • 10
  • 29
dotty
  • 40,405
  • 66
  • 150
  • 195
33

From Copy images from url to server, delete all images after

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);       
Community
  • 1
  • 1
Michael Robinson
  • 29,278
  • 12
  • 104
  • 130
17
$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);

you must have allow_url_fopen set to on

Shakti Singh
  • 84,385
  • 21
  • 134
  • 153
9

This SO thread will solve your problem. Solution in short:

$url = 'http://www.google.co.in/intl/en_com/images/srpr/logo1w.png';
$img = '/my/folder/my_image.gif';
file_put_contents($img, file_get_contents($url));
Community
  • 1
  • 1
Thariama
  • 50,002
  • 13
  • 138
  • 166
4
$url = "http://www.example/images/image.gif";
$save_name = "image.gif";
$save_directory = "/var/www/example/downloads/";

if(is_writable($save_directory)) {
    file_put_contents($save_directory . $save_name, file_get_contents($url));
} else {
    exit("Failed to write to directory "{$save_directory}");
}
HenryW
  • 3,551
  • 1
  • 23
  • 23
Shay Anderson
  • 964
  • 6
  • 5