用php获取远程图片并把它保存到本地的代码
2018-10-15 18:00
                        <?php 
functionGrabImage($url,$filename=){ 
if($url==):returnfalse;endif; 
if($filename==){ 
$ext=strrchr($url,.); 
if($ext!=.gif&&$ext!=.jpg):returnfalse;endif; 
$filename=date(dMYHis).$ext; 
} 
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; 
} 
$img=GrabImage(
if($img):echo<pre><imgsrc=.$img.></pre>;else:echofalse;endif; 
?> 
dedecms中的: 
if(!empty($saveremoteimg)) 
{ 
$body=stripslashes($body); 
$img_array=array(); 
preg_match_all(/(srcSRC)=[\]{0,}(http:\/\/(.*)\.(gifjpgjpegbmppng))/isU,$body,$img_array); 
$img_array=array_unique($img_array[2]); 
set_time_limit(0); 
$imgUrl=$img_dir./.strftime(%Y%m%d,time()); 
$imgPath=$base_dir.$imgUrl; 
$milliSecond=strftime(%H%M%S,time()); 
if(!is_dir($imgPath))@mkdir($imgPath,0777); 
foreach($img_arrayas$key=>$value) 
{ 
$value=trim($value); 
$get_file=@file_get_contents($value); 
$rndFileName=$imgPath./.$milliSecond.$key...substr($value,-3,3); 
$fileurl=$imgUrl./.$milliSecond.$key...substr($value,-3,3); 
if($get_file) 
{ 
$fp=@fopen($rndFileName,w); 
@fwrite($fp,$get_file); 
@fclose($fp); 
} 
$body=ereg_replace($value,$fileurl,$body); 
} 
$body=addslashes($body); 
} 
                    
下一篇:PHP中list方法用法示例