Home >Backend Development >PHP Tutorial >Sample code for PHP to collect web page images and save them locally

Sample code for PHP to collect web page images and save them locally

WBOY
WBOYOriginal
2016-07-25 08:55:06987browse
  1. /**
  2. * Save web page files locally (for collecting pictures)
  3. * edit: bbs.it-home.org
  4. * @param file path $sUrl
  5. * @param save local path $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. * Get the file name
  16. *
  17. * @param web page 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<100;$i++)
  29. {
  30. $sUrl = "http://bbs.it-home.org/red/marker$i.png";
  31. download_file($sUrl,$sPath);
  32. }
  33. ?>
复制代码


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn