Posted By

Nettuts on 06/25/10


Tagged

curl php images


Versions (?)

Who likes this?

17 people have marked this snippet as a favorite

Hollow
hmvrulz
seanpowell
barbietunnie
samkamerer
diegotrap
petemcbride
soyoh
mikael12
prit
qubestream
benediktvaldez
tewoos
wirenaught
voove
yusege
tux-world


Save Any Website Image to your Server


 / Published in: PHP
 

  1. <?php
  2.  
  3. function saveImage($path) {
  4.  
  5. if ( !preg_match('/\/([^\/]+\.[a-z]{3,4})$/i', $path, $matches) ) die('Use image please');
  6.  
  7. $image_name = strToLower($matches[1]);
  8.  
  9. $ch = curl_init ($path);
  10. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  11. curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
  12.  
  13. $img = curl_exec ($ch);
  14. curl_close ($ch);
  15.  
  16. $fp = fopen($image_name,'w');
  17. fwrite($fp, $img);
  18. fclose($fp);
  19.  
  20. }
  21.  
  22. saveImage('http://nettuts.s3.cdn.plus.org/513_jsFromNull/javascript-from-null.jpg');
  23.  
  24. ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: Nettuts on June 25, 2010

This is a handy little function that I needed for a project several months ago. By calling the "saveImage" function, and passing in a path to an image, it will save the image to your server. Make sure that you modify the "fopen" section if the image should be stored in some specific folder.

You need to login to post a comment.