/**
* 下载图片到本地
* @param $imgUrl string 网络路径
* @param $localPath string 保存本地绝对路径
*/
protected static function downloadImage($imgUrl, $localPath)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $imgUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$content = curl_exec($ch);
curl_close($ch);
if (!file_exists(dirname($localPath))) {
mkdir(dirname($localPath), 0755, true);
}
//文件大小
$fp2 = @fopen($localPath, 'w');
fwrite($fp2, $content);
fclose($fp2);
unset($content, $url);
}
下载图片到本地
最新推荐文章于 2024-10-17 13:20:20 发布