Maison  >  Article  >  développement back-end  >  PHP采集网页图片保存到本地的示例代码

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

WBOY
WBOYoriginal
2016-07-25 08:55:06920parcourir
  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. ?>
复制代码


Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn