0 - Be sure you've read PHP manual to see all the amazing built-in functions PHP has.
1 - Build a local path for the image, you may use preg_replace
to sanitize the URL
2 - Check the image hasn't already been downloaded using file_exists
, if so, load it; else download it
3 - Use file_get_contents
to retrieve the image (cURL
would be uselessly heavier)
4 - Save it to a local file using file_put_contents
foreach( $html->find('.featured img') as $image )
{
$imageSrc = $image->src;
$imageUri = $this->rel2abs($imageSrc, $sourceURI);
$imageLocalPath = 'getImages/'.preg_replace('/[^a-z0-9-.]/i', '-', $imageUri);
if (!file_exists($imageLocalPath))
{
$imageData = file_get_contents($imageUri, false, $streamContext);
file_put_contents($imageLocalPath, $imageData);
}
else
$imageData = file_get_contents($imageLocalPath);
}
Notes:
- You need
rel2abs
to resolve relative URIs, or any appropriate pecl extension.
- getImages/ will put all images in a subfolder: you need to manually create that subfolder, or check its existence in the PHP code and create it if needed
- $imageData contains the raw data of the image, you may which to use
imagecreatefromstring
to load the corresponding Gd image.
- Take care: you're downloading stuff from a distant webpage, so you must trust it well. One could add a tag like
<div class="featured"><img src="http://evil.com/your-heart-will-bleed.php"/></div>
in the html page, and the evil php file will be downloaded. Worst, it may be executed by visiting your website http://mywebsite.com/getImages/your-heart-will-bleed.php
.