首頁  >  文章  >  後端開發  >  PHP采集网页图片保存到本地的示例代码

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

WBOY
WBOY原創
2016-07-25 08:55:06915瀏覽
  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. ?>
复制代码


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn