In this example, the file_get_contents () function is used to capture the image content and then saved to the local server using fopen, then, replace the Address url to implement a complete page collection function and save the address locally. Let's take a look at the example below. In this example, the file_get_contents () function is used to capture the image content and then saved to the local server using fopen, then, replace the Address url to implement a complete page collection function and save the address locally. Let's take a look at the example below.
Script ec (2); script
First, let's give an example. The Code is as follows:
The Code is as follows: |
|
$ Text = file_get_contents ("http://www.111cn.net "); // Obtain all the img tags and store them to the two-dimensional array match Preg_match_all ('/<[img | IMG]. *? Src = [\ '| \ "] (. *? (? : [\. Gif | \. jpg]) [\ '| \ "]. *? [\/]?> /', $ Text, $ match ); // Print the match $ Houzhui = "./tp/". time (). rand (). ".". png; $ Yuanname = getImage ($ match [1] [2], $ houzhui, tp ); // How to download images Function getImage ($ url, $ filename = '', $ type = 0 ){ If ($ url = '') {return false ;} If ($ filename = ''){ $ Ext = strrchr ($ url ,'.'); If ($ ext! Using '.gif '& $ ext! Else '.jpg ') {return false ;} $ Filename = time (). $ ext; } // File storage path If ($ type ){ $ Ch = curl_init (); $ Timeout = 5; Curl_setopt ($ ch, CURLOPT_URL, $ url ); Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 ); Curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, $ timeout ); $ Img = curl_exec ($ ch ); Curl_close ($ ch ); } Else { Ob_start (); Readfile ($ url ); $ Img = ob_get_contents (); Ob_end_clean (); } $ Size = strlen ($ img ); // File Size $ Fp2 = @ fopen ($ filename, 'A '); Fwrite ($ fp2, $ img ); Fclose ($ fp2 ); Return $ filename; } ?> |
The core code for case analysis is as follows:
The Code is as follows: |
|
$ Text = file_get_contents ("http://www.111cn.net "); // Obtain all the img tags and store them to the two-dimensional array match Preg_match_all ('/<[img | IMG]. *? Src = [\ '| \ "] (. *? (? : [\. Gif | \. jpg]) [\ '| \ "]. *? [\/]?> /', $ Text, $ match ); |
This regular expression means that all images, whether http or directly/aa/aa.gif files, will be automatically captured and saved to the address. However, there may be some problems. The image address has not been completely supplemented, for example, we have a/a.gif so that we can not find the picture, must be a http://www.111cn.net/a/a.gif so that you can download, therefore, we need to perform two Processing Methods: one is to basically process the original
The Code is as follows: |
|
// File storage path If ($ type ){ $ Ch = curl_init (); $ Timeout = 5; Curl_setopt ($ ch, CURLOPT_URL, $ url ); Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 ); Curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, $ timeout ); $ Img = curl_exec ($ ch ); Curl_close ($ ch ); |
The above $ url parameters for address completion, such as my collection is the http://www.111cn.net, then the Address Auto completion for the absolute path, the other way is to use the modify Regular Expression
The Code is as follows: |
|
Preg_match_all ("/(src | SRC) = [\" | '|] {0,} (http :\/\/(. *)\. (gif | jpg | jpeg | png)/isU ", $ body, $ img_array ); |
In this way, only image files starting with http are obtained.