图片Url下载到本地 php 下载图片到本地

来源:csdn博客 分类: 文章浏览史 发布时间:2020-03-21 14:37:03 最后更新:2020-03-21 浏览:460
转载声明:
本文为摘录自“csdn博客”,版权归原作者所有。
温馨提示:
为了更好的体验,请点击原文链接进行浏览
摘录时间:
2020-03-21 14:37:03
<?php

function down_images($url) {


	$header = array("Connection: Keep-Alive", "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Pragma: no-cache", "Accept-Language: zh-Hans-CN,zh-Hans;q=0.8,en-US;q=0.5,en;q=0.3", "User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:29.0) Gecko/20100101 Firefox/29.0");

	$ch = curl_init();

	curl_setopt($ch, CURLOPT_URL, $url);

	//curl_setopt($ch, CURLOPT_HEADER, $v);

	curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

	curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');

	$content = curl_exec($ch);

	$curlinfo = curl_getinfo($ch);

	//print_r($curlinfo);

	//关闭连接

	curl_close($ch);



	if ($curlinfo['http_code'] == 200) {

	if ($curlinfo['content_type'] == 'image/jpeg') {

	$exf = '.jpg';

	} else if ($curlinfo['content_type'] == 'image/png') {

	$exf = '.png';

	} else if ($curlinfo['content_type'] == 'image/gif') {

	$exf = '.gif';

	}

	//存放图片的路径及图片名称  *****这里注意 你的文件夹是否有创建文件的权限 chomd -R 777 mywenjian

	$filename = date("YmdHis") . uniqid() . $exf;//这里默认是当前文件夹,可以加路径的 可以改为$filepath = '../'.$filename
	$filepath = 'images/'.$filename;
	var_dump($content);
	$res = file_put_contents($filepath, $content);
	//$res = file_put_contents($filename, $content);//同样这里就可以改为$res = file_put_contents($filepath, $content);
	//echo $filepath;
        echo $res;
	}

}

$url = "https://hrtvoss.oss-cn-beijing.aliyuncs.com/20160104115712_35150.png";
down_images($url);

?>

 

php技术微信