如何使用PHP从单个网页下载图像


How to download images from a single webpage using PHP

我创建了这个脚本来从单个网页获取图像的来源,现在,我想使用curl将所有这些图像下载到我的服务器。

<?php 
// Create DOM from URL or file
require_once 'library/simple_html_dom.php';
$html = file_get_html('http://adamkhoury.com/');
// Find all images 
foreach($html->find('img') as $element) //every image found is declared as $element
       echo $element->src . '<br>';
?>

就这么简单

file_put_contents("filename.extension", file_get_contents($element->src));

PHP中有file_get_contents()函数用于获取外部内容,file_put_contents()函数用于将内容存储在文件中…

使用file_get_contents()下载图像,并使用file_put_contents()存储图像:

$image = file_get_contents($element->src);
file_get_contents("image.jpg",$image);

当然,php中还有一个方法叫做cURL,用来下载外部数据…