php下载文件并保存到指定文件夹

php下载图片并保存到指定文件夹

function GrabImage($url, $dir, $filename=''){
	if(empty($url)){
		return false;
	}
	$ext = strrchr($url, '.');
	if($ext != '.gif' && $ext != ".jpg" && $ext != ".bmp"){
		echo "格式不支持!";
		return false;
	}
	
	$dir = realpath($dir);
	//目录+文件
	$filename = (empty($filename) ? '/'.time().''.$ext : '/'.$filename);
	$filename = $dir . $filename;
	//开始捕捉 
	ob_start(); 
	readfile($url); 
	$img = ob_get_contents(); 
	ob_end_clean(); 
	$size = strlen($img); 
	$fp2 = fopen($filename , "a"); 
	fwrite($fp2, $img); 
	fclose($fp2); 
	return $filename; 
}

//测试 
$imgpath="https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1483602960&di=f93d78756010023602ecb700b802658f&src=http://pic.90sjimg.com/back_pic/u/00/38/54/05/560412efec2d6.jpg";
GrabImage($imgpath,"D://img");//(图片地址,存放目录,存放显示文件名称);




你可能感兴趣的:(php,php)