CURL下载远程图片到本地

2019-09-19 10:50:44 浏览数 (1)

代码语言:javascript复制
//下载远程图片
$ch = curl_init();
$fp=fopen('./test/'.basename($imgurl), 'w');
curl_setopt($ch, CURLOPT_URL,$imgurl);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 100);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_FILE, $fp);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
$error = curl_error($ch);
fclose($fp);
$size = filesize('./test/'.basename($imgurl));
if ($size != $info['size_download']) {
    echo "下载失败"; echo $error;
} else {
    echo "下载成功";
}
curl_close($ch);
url

0 人点赞