In this article, an ajax instance provides a section about saving an image to the local address through the image address. Here we can directly read the image through readfile and save it through fopen.
Instance
| The Code is as follows: |
Copy code |
|
<? Php
/**
* Download the image to the local device through the remote url of the image
* @ Param: $ url indicates the remote link of the image.
* @ Param: $ filename indicates the file name saved after the image is downloaded.
*/
Function GrabImage ($ url, $ filename ){
If ($ url = ""): return false; endif;
Ob_start ();
Readfile ($ url );
$ Img = ob_get_contents ();
Ob_end_clean ();
$ Size = strlen ($ img );
// "../Images/books/" is the storage directory, and $ filename is the file name.
$ Fp2 = @ fopen (".../../images/books/". $ filename, "");
Fwrite ($ fp2, $ img );
Fclose ($ fp2 );
Return $ filename;
}
?> |
The function returns the name saved after the image is downloaded. You only need to store the image directory + image name in the database.