Posted By

goo on 01/14/13


Tagged

download images Website


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

Priestd09


Download and save images from a page using cURL


 / Published in: PHP
 

URL: http://stackoverflow.com/questions/7747875/grab-download-images-from-multiple-pages-using-php-preg-match-all-curl

Here is a set of functions that can be very useful: Give this script the url of a webpage, and it will save all images from this page on your server.

  1. function getImages($html) {
  2. $matches = array();
  3. $regex = '~http://somedomain.com/images/(.*?)\.jpg~i';
  4. preg_match_all($regex, $html, $matches);
  5. foreach ($matches[1] as $img) {
  6. saveImg($img);
  7. }
  8. }
  9.  
  10. function saveImg($name) {
  11. $url = 'http://somedomain.com/images/'.$name.'.jpg';
  12. $data = get_data($url);
  13. file_put_contents('photos/'.$name.'.jpg', $data);
  14. }
  15.  
  16. $i = 1;
  17. $l = 101;
  18.  
  19. while ($i < $l) {
  20. $html = get_data('http://somedomain.com/id/'.$i.'/');
  21. getImages($html);
  22. $i += 1;
  23. }

Report this snippet  

You need to login to post a comment.