Grab/download images from multiple pages using php preg_match_all & cURL Grab/download images from multiple pages using php preg_match_all & cURL curl curl

Grab/download images from multiple pages using php preg_match_all & cURL


This was pretty confusing, because it sounded like you were only interested in saving one image per page. But then the code makes it look like you're actually trying to save every image on each page. So it's entirely possible I completely misunderstood... But here goes.

Looping over each page isn't that difficult:

$i = 1;$l = 101;while ($i < $l) {    $html = get_data('http://somedomain.com/id/'.$i.'/');    getImages($html);    $i += 1;}

The following then assumes that you're trying to save all the images on that particular page:

function getImages($html) {    $matches = array();    $regex = '~http://somedomain.com/images/(.*?)\.jpg~i';    preg_match_all($regex, $html, $matches);    foreach ($matches[1] as $img) {        saveImg($img);    }}function saveImg($name) {    $url = 'http://somedomain.com/images/'.$name.'.jpg';    $data = get_data($url);    file_put_contents('photos/'.$name.'.jpg', $data);}