Php uses regular expressions to download the remote image function code. This program is actually an important part of the "thief Program. This program is just a part of downloading remote images. The program is easy to write and comments are made in most parts. This program is actually an important part of the "thief Program. This program is just a part of downloading remote images. The program is easy to write and comments are made in most parts.
The code is as follows:
If (preg_match_all ("/http: // [^" '{}}.jpg |. gif |. jpeg |. png] +/ui ", stripcslashes ($ content), $ aliurl )){
$ I = 0; // multiple files ++
While (list ($ key, $ v) = each ($ aliurl [0]) {
// Echo $ v ."
";
$ Filetype = pathinfo ($ v, PATHINFO_EXTENSION); // Get the suffix
$ Ff = @ file_get_contents ($ v); // Get the binary file content
If (! Stripos ($ v, "jb51.net") {// determines whether the image is under your website.
If (! Empty ($ ff) {// perform the following operations when the file is obtained
$ Dir = "upload/". date ("Ymd"). "/"; // specify the new storage path
If (! File_exists ($ dir) {// determines whether the directory exists
@ Mkdir ($ dir, 511, true); // create a multi-level Directory. if 511 is converted to decimal, 777 has the executable permission.
}
$ Nfn = $ dir. date ("Ymdhis"). $ I. ".". $ filetype; // new name of the build file
$ Nf = @ fopen ($ nfn, "w"); // create a file
Fwrite ($ nf, $ ff); // write a file
Fclose ($ nf); // close the file
$ I ++; // multiple files ++
Echo "";
$ Content = str_replace ($ v, $ nfn, $ content); // replace the parameters in content
} Else {// if no image is obtained, replace it with the default image.
$ Content = str_replace ($ v, "/upload/201204/20120417213810742 .gif", $ content); // replace the parameters in content
}
}
}
}
PHP implementation code for downloading images to a local place using regular expressions
The code is as follows:
/*
Author: ssh_kobe
Data: 20110602
Snapshot age: if the image path on the webpage is not an absolute path, it cannot be captured.
*/
Set_time_limit (0); // capture is not subject to the time limit
$ URL = 'http: // pp.baidu.com/'#//any URL
Get_pic ($ URL );
Function get_pic ($ pic_url ){
// Obtain the binary stream of the image
$ Data = CurlGet ($ pic_url );
/* Use a regular expression to obtain the image link */
$ Pattern_src = '/<[img | IMG]. *? Src = [\ '| \ "] (. *? (? : [\. Gif | \. jpg]) [\ '| \ "]. *? [\/]?> /';
$ Num = preg_match_all ($ pattern_src, $ data, $ match_src );
$ Arr_src = $ match_src [1]; // Obtain the image array
Get_name ($ arr_src );
Echo"
Finished !!! ";
Return 0;
}
/* Obtain the image type and save it to the same directory as the file */
Function get_name ($ pic_arr)
{
// Image type
$ Pattern_type = '/(/. (jpg | bmp | jpeg | gif | png ))/';
Foreach ($ pic_arr as $ pic_item) {// cyclically retrieve the address of each graph
$ Num = preg_match_all ($ pattern_type, $ pic_item, $ match_type );
$ Pic_name = get_unique (). $ match_type [1] [0]; // name the timestamp in microseconds
// Save the image as a stream
$ Write_fd = @ fopen ($ pic_name, "wb ");
@ Fwrite ($ write_fd, CurlGet ($ pic_item ));
@ Fclose ($ write_fd );
Echo "[OK]...! ";
}
Return 0;
}
// Obtain the unique ID in microseconds
Function get_unique (){
List ($ msec, $ sec) = explode ("", microtime ());
Return $ sec. intval ($ msec * 1000000 );
}
// Capture webpage content
Function CurlGet ($ url ){
$ Url = str_replace ('&', '&', $ url );
$ Curl = curl_init ();
Curl_setopt ($ curl, CURLOPT_URL, $ url );
Curl_setopt ($ curl, CURLOPT_HEADER, false );
// Curl_setopt ($ curl, CURLOPT_REFERER, $ url );
Curl_setopt ($ curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; SeaPort/1.2; Windows NT 5.1; SV1; InfoPath.2 )");
Curl_setopt ($ curl, CURLOPT_COOKIEJAR, 'cookie.txt ');
Curl_setopt ($ curl, CURLOPT_COOKIEFILE, 'cookie.txt ');
Curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ curl, CURLOPT_FOLLOWLOCATION, 0 );
$ Values = curl_exec ($ curl );
Curl_close ($ curl );
Return $ values;
}
?>
Bytes. This program is just a part of downloading remote images. The program is simple to write and comments are made in most parts ....