PHP method for obtaining remote images and saving them to a local device

Source: Internet
Author: User
This article mainly introduces how PHP obtains remote images and saves them to a local device. It involves related php Remote File Operations. For more information, see

This article mainly introduces how PHP obtains remote images and saves them to a local device. It involves related php Remote File Operations. For more information, see

This example describes how PHP retrieves remote images and saves them to a local device. Share it with you for your reference. The specific implementation method is as follows:

<? Phpfunction GrabImage ($ url, $ filename = "") {if ($ url = ""): return false; endif; // if the $ url address is empty, exit if ($ filename = "") {// if no new file name is specified $ ext = strrchr ($ url ,". "); // get the $ url image format if ($ ext! = ". Gif" & $ ext! = ". Jpg "): return false; endif; // exit $ filename = date (" dMYHis ") if the image format is not "). $ ext; // name the new file name by the hour, minute, and second of the day,} ob_start (); // open the output readfile ($ url ); // output image file $ img = ob_get_contents (); // get the browser output ob_end_clean (); // clear the output and close $ size = strlen ($ img ); // get the image size $ fp2 = @ fopen ($ filename, "a"); fwrite ($ fp2, $ img); // write the image file to the current directory, and rename fclose ($ fp2); return $ filename; // return the new file name} $ img = GrabImage ("http://imgsrc.baidu.com/baike/abpic/item/6648d73db0edd1e89f3d62f7.jpg", ""); if ($ img ): echo'

'; // If the returned value is true, the else: echo "false"; endif; // otherwise, the output fails to be collected. 

I hope this article will help you with php programming.

,

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.