Php saves network images and automatically collects and saves remote images and files-PHP source code

Source: Internet
Author: User
Ec (2); * $ dirwww.111cn.net; & nbsp; if (strstr ($ src, & quot;) & amp ;! Strstr ($ src, $ _ SERVER [HTTP_HOST]) {& nbsp; $ srcgetimg ($ src); & nbsp ;}& nbsp; & nbsp; functiongetimg (script ec (2); script

*/
$ Dir = '/www.111cn.net ';

If (strstr ($ src, "http ://")&&! Strstr ($ src, $ _ SERVER ['HTTP _ host']) {
$ Src = getimg ($ src );
}

Function getimg ($ l1 ){
$ L2 = $ dir. '/'. substr (md5 ($ l1), 10, 10). strrchr ($ l1 ,".");
// The file exists and the address is returned directly.
If (file_exists ($ l2 )){
// Echo "exits ...";
Return $ l2;
}

// Get the file and return the new path
$ Img = file_get_contents ($ l1 );
If ($ img ){
If (! Is_dir ($ dir )){
@ Mkdir ($ dir );
}
Savefile ($ l2, $ img );
// Echo "file_get ..";
Return $ l2;
}
}

// Save the file (file, [content])

Function savefile ($ l1, $ l2 = ''){
If (function_exists (file_put_contents )){
File_put_contents ($ l1, $ l2 );
} Else {
$ Fp = @ fopen ($ l1, 'wb ');
@ Fwrite ($ fp, $ l2 );
Fclose ($ fp );
}
}


/*

The strrchr () function is used to locate the last occurrence of a string in another string and return all characters from this position to the end of the string.
The strstr () function searches for the first occurrence of a string in another string. This function returns the rest of the string (from the matching point ). If the searched string is not found, false is returned.
File_exists: determines whether a file exists. If yes, true or 1 is returned. If no, false or 0 is returned.
The file_get_contents () function reads the entire file into a string.
Mkdir: directory created. If the directory is successfully created, true or false
The file_put_contents () function writes a string to a file, just like calling the fopen (), fwrite (), and fclose () functions in turn.

This program is the homepage to determine whether the read address is network or local. If it is the image address of this server, it will not be processed. Otherwise, the remote server image or file will be downloaded to the local server.

*/


// This article was originally posted on www.111cn.net and indicated the source

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.