Created
December 3, 2013 16:43
-
-
Save enopanen/7772600 to your computer and use it in GitHub Desktop.
Download and save images from a page using 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
$i = 1; | |
$l = 101; | |
while ($i < $l) { | |
$html = get_data('http://somedomain.com/id/'.$i.'/'); | |
getImages($html); | |
$i += 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment