Sample code for saving web images collected by PHP to a local device

Source: Internet
Author: User
Sample code for saving web images collected by PHP to a local device

  1. /**
  2. * Save the webpage file to a local device (used to collect images)
  3. * Edit: bbs.it-home.org
  4. * @ Param file path $ sUrl
  5. * @ Param save the 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 webpage 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 I <100; $ I ++)
  29. {
  30. $ SUrl = "http://bbs.it-home.org/red/marker$ I .png ";
  31. Download_file ($ sUrl, $ sPath );
  32. }
  33. ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.