test();die;
// 下载图片文件
function test() {
set_time_limit(0);
$pageTotal = 100;
// for ($page = 1; $page <= $pageTotal; $page ++) {
$url = "https://www.baidu.com/img/flexible/logo/pc/result.png";// 图片地址
download($url);// 执行下载
// }
}
/*url 下载地址
path 文件保存地址*/
function download($url, $path = 'images/') {
if (!file_exists($path)) {
createDirectory($path);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
$file = curl_exec($ch);
curl_close($ch);
$filename = pathinfo($url, PATHINFO_BASENAME);
$resource = fopen($path . $filename, 'a');
fwrite($resource, $file);
fclose($resource);
}
// 判断文件夹是否存在,不存在则创建
function createDirectory($dir) {
return is_dir ( $dir ) or createDirectory(dirname( $dir )) and (mkdir ( $dir , 0777) && chmod($dir, 0777));
}
01-06
506
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)