class Spider {
public function downloadImage($url, $path='imagesjpg/')
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
$file = curl_exec($ch);
curl_close($ch);
$this->saveAsImage($url, $file, $path);
}
private function saveAsImage($url, $file, $path)
{
$filename = pathinfo($url, PATHINFO_BASENAME);
$resource = fopen($path . $filename, 'a');
fwrite($resource, $file);
fclose($resource);
}
}
$spider = new Spider();
for ($i=1;$i<300;$i++) {//直接循环生成路径然后下载到本地同目录下(./imagesjpg/)中
$url="http://***/".$i.".jpg";//图片路径
$spider->downloadImage($url);
}
php远程下载网络图片
最新推荐文章于 2024-05-21 13:13:48 发布