在做网站QQ登陆或者微博登陆的时候,我们要从QQ或者微博获取用户的头像,那么怎么用PHP保存头像到本地了?
代码如下:
<?php $url='http://www.haoshu888.com/wp-content/themes/frontopen/images/logo.gif'; $filename = './images/haoshu888.gif'; //表示保存到当前目录下的images目录下 $type=0; if($url==''){return false;} //文件保存路径 if($type){ $ch=curl_init(); $timeout=5; curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout); $img=curl_exec($ch); curl_close($ch); }else{ 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; ?>
转载请注明: haoshu发表于浩叔逛逛>>https://www.haoshu888.com/php/434.html