Tuesday, May 21, 2013

Saving image from external url using PHP

There exists several methods to save image from external url to the remote server such as cURL, combination of file_put_contents and file_get_contents. But the most efficient method is to use copy() function.
copy() - Makes a copy of the file source to dest.
  copy(string $source,string $dest)
where, 
  $source = Path to the source file.
  $dest = The destination path.
If the destination file already exists, it will be overwritten. Returns TRUE on success or FALSE on failure.
Example
  copy('http://example.com/image.php', 'local/folder/flower.jpg');
Note: Make sure allow_url_fopen is on.
Read more on http://php.net/manual/en/function.copy.php for detail explanation.

No comments:

Post a Comment