Download and save images from a page using cURL


/ Published in: PHP
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  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. }

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.