PHP采集网页图片并保存至本地

[php] view plaincopy
 
  1. <?php  
  2. /** 
  3.  * 保存网页文件到本地(用于采集图片) 
  4.  * 
  5.  * @param 文件路径 $sUrl 
  6.  * @param 保存本地路径 $sSavePath 
  7.  * @return boolean 
  8.  */  
  9. function download_file($sUrl,$sSavePath='')  
  10. {  
  11.     $sFileName = GetUrlFileExt($sUrl);  
  12.     $c = file_get_contents($sUrl);  
  13.     return file_put_contents($sSavePath.'/'.$sFileName,$c);  
  14. }  
  15.   
  16. /** 
  17.  * 获取文件名 
  18.  * 
  19.  * @param 网页URL $sUrl 
  20.  * @return string 
  21.  */  
  22. function GetUrlFileExt($sUrl)  
  23. {  
  24.     $aAry = parse_url($sUrl);  
  25.     $sFile = basename($aAry['path']);  
  26.     $sExt = explode('.',$sFile);  
  27.     return $sExt[0].'.'.$sExt[1];  
  28. }  
  29.   
  30. $sPath = "D:/marker_imgs";  
  31. for($i=1;$i<100;$i++)  
  32. {  
  33.     $sUrl = "http://www.iwaimai.net/gmap/images/markers/red/marker$i.png";  
  34.     download_file($sUrl,$sPath);  
  35. }  
  36. ?>  
原文地址:https://www.cnblogs.com/txd66/p/3234506.html