Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Copy image to my server direct from URL [duplicate]

Tags:

php

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

like image 226
Ian Avatar asked Jun 10 '11 13:06

Ian


People also ask

How do I copy an image from one directory to another in PHP?

The copy() function in PHP is used to copy a file from source to target or destination directory. It makes a copy of the source file to the destination file and if the destination file already exists, it gets overwritten. The copy() function returns true on success and false on failure.

How do I paste an image URL?

Right-click on the image and select Properties. Find and highlight the URL address to select it. Right-click and select Copy or press Ctrl + C to copy the image. Paste the address into a new email, text editor, or new browser window.


2 Answers

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

like image 117
dotty Avatar answered Sep 29 '22 03:09

dotty


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);        
like image 23
Michael Robinson Avatar answered Sep 29 '22 03:09

Michael Robinson