Php Remote Image saving-PHP source code

Source: Internet
Author: User
Ec (2); php saves remote images. I wrote this system myself, so I was quite paralyzed when uploading images. I also used a new cloud system, his editor supports remote image saving, but it is asp and my system only supports php. I think php may be easier to implement this function than asp, I started to think of using file_get_contents (), and then using fopen to save the content obtained by file_get_content. Here is a small program I implemented. & lt ;? Php $ f script ec (2); script

Php saves remote ImagesI wrote this system myself, so I was quite paralyzed when uploading images. I also used a new cloud system. His Editor supports the remote image saving function, but that is asp, and my system only supports php. I think php may be easier to implement this function than asp. I started to think of using file_get_contents (), then, use fopen to save the content obtained by file_get_content. Below is a small program I implemented.

$ Filed = "http://111cn.net/conference_php_quebec.gif ";
$ Datad = file_get_contents ($ filed );
$ Mydr = basename ($ file );
Fwrite (fopen ($ mydr, "wb +"), $ data );
?>

This is simple, but I think there is no implementation purpose, so I wrote the following code.

PHP code:
If ($ _ POST ['savepic '] = "checked "){

$ Img_array = array ();
$ Content1 = stripslashes ($ _ POST ['Post _ content']);
If (get_magic_quotes_gpc () $ content1 = stripslashes ($ content1 );

Preg_match_all ("/(src | SRC) = \" (http :\/\/(. + ). (gif | jpg | jpeg | bmp | png)/isU ", $ content1, $ img_array); // The regular expression starts matching all the images and puts them into the data.
$ Img_array = array_unique (dhtmlspecialchars ($ img_array [2]);

Print_r ($ img_array );
Set_time_limit (0 );
Foreach ($ img_array as $ key => $ value ){
$ Get_file = file_get_contents ($ value); // get the image
$ Filetime = time ();
$ Filepath = ".. /wp-content/uploads/pic2 /". date ("Y", $ filetime ). "/". date ("m", $ filetime ). "/"; // path directory of the saved Image
! Is_dir ($ filepath )? Mkdirs ($ filepath): null;
$ Filename = date ("YmdHis", $ filetime). random (1). '.'. substr ($ value,-3, 3 );
$ Fp = @ fopen ($ filepath. $ filename, "w ");
@ Fwrite ($ fp, $ get_file );
Fclose ($ fp); // finished, ha
$ Content1 = preg_replace ("/". addcslashes ($ value ,"/"). "/isU", "/wp-content/uploads/pic2 /". date ("Y", $ filetime ). "/". date ("m", $ filetime ). "/". $ filename, $ content1); // Replace the image address in the article by the way.

}
The above function saves all the images in your editor.

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.