PHP下载远程图片的3个方法

From: http://blog.csdn.net/iefreer/article/details/46930239

直接上代码

<?php

function dlfile1($file_url, $save_to)
{
	$in = fopen($file_url, "rb");
	$out = fopen($save_to, "wb");
	while ($chunk = fread($in,8192))
	{
		fwrite($out, $chunk, 8192);
	}
	fclose($in);
	fclose($out);
}

function dlfile_curl($file_url, $save_to)			// 不支持https
{
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_POST, 0); 
	curl_setopt($ch,CURLOPT_URL, $file_url); 
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
	$file_content = curl_exec($ch);
	curl_close($ch);
	if(!$file_content)
	{
		echo "download file failed";
		return;
	}

	$downloaded_file = fopen($save_to, 'w');
	fwrite($downloaded_file, $file_content);
	fclose($downloaded_file);
}

function dlfile3($file_url, $save_to)
{
	$content = file_get_contents($file_url);
	file_put_contents($save_to, $content);
}


$url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=gQFM8TwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyM1lwVXBCRWNkQ20xNmV5TDFwMUQAAgSO1QdZAwQAjScA";

//$url = 'http://avatar.csdn.net/9/D/D/1_iefreer.jpg';

dlfile3($url, 'aaa.jpg');

  

原文地址:https://www.cnblogs.com/joeblackzqq/p/6794569.html