Heim  >  Artikel  >  Backend-Entwicklung  >  PHP采集网页图片保存到本地的示例代码

PHP采集网页图片保存到本地的示例代码

WBOY
WBOYOriginal
2016-07-25 08:55:06915Durchsuche
  1. /**
  2. * 保存网页文件到本地(用于采集图片)
  3. * edit: bbs.it-home.org
  4. * @param 文件路径 $sUrl
  5. * @param 保存本地路径 $sSavePath
  6. * @return boolean
  7. */
  8. function download_file($sUrl,$sSavePath='')
  9. {
  10. $sFileName = GetUrlFileExt($sUrl);
  11. $c = file_get_contents($sUrl);
  12. return file_put_contents($sSavePath.'/'.$sFileName,$c);
  13. }
  14. /**
  15. * 获取文件名
  16. *
  17. * @param 网页URL $sUrl
  18. * @return string
  19. */
  20. function GetUrlFileExt($sUrl)
  21. {
  22. $aAry = parse_url($sUrl);
  23. $sFile = basename($aAry['path']);
  24. $sExt = explode('.',$sFile);
  25. return $sExt[0].'.'.$sExt[1];
  26. }
  27. $sPath = "D:/marker_imgs";
  28. for($i=1;$i{
  29. $sUrl = "http://bbs.it-home.org/red/marker$i.png";
  30. download_file($sUrl,$sPath);
  31. }
  32. ?>
复制代码


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn