PHP Save the network picture, automatically collect and save remote pictures and files

Source: Internet
Author: User
Tags create directory save file

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

if (Strstr ($src, "http://") &&!strstr ($src, $_server[' Http_host ')) {
$SRC = getimg ($SRC);
}

function getimg ($l 1) {
$l 2 = $dir. substr (MD5 ($l 1), 10,10). STRRCHR ($l 1, ".");
File exists, return address directly
if (File_exists ($l 2)) {
echo "Exits ...";
return $l 2;
}

Start getting the file and return to the new path
$img = file_get_contents ($l 1);
if ($img) {
if (!is_dir ($dir)) {
@mkdir ($dir);
}
SaveFile ($l 2, $img);
echo "File_get";
return $l 2;
}
}

Save file (file, [content])

function SaveFile ($l 1, $l 2 = ') {
if (function_exists (file_put_contents)) {
File_put_contents ($l 1, $l 2);
} else{
$fp = @fopen ($l 1, ' WB ');
@fwrite ($FP, $l 2);
Fclose ($FP);
}
}


/*

The STRRCHR () function finds the last occurrence of a string in another string and returns all characters from that position to the end of the string
The Strstr () function searches for a string that appears for the first time in another string. The function returns the remainder of the string (from a match point). returns false if the searched string is not found.
File_exists determines whether the file exists, is returned ture or 1 does not return FALSE or 0
The file_get_contents () function reads the entire file into a string.
mkdir Create directory, success Ture no false
The file_put_contents () function writes a string to the file and calls fopen (), fwrite (), and fclose () in turn.

This procedure is the home page to determine whether we read the address is the network or local, if the picture address of the server is not processed otherwise the remote server pictures or files downloaded to the local server.

*/


This article original in www.111cn.net reprint to indicate the origin

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.