Which of the following code is wrong?

Source: Internet
Author: User
Which of the following code is wrong? ** & Nbsp; * & nbsp; download remote images & nbsp; * & nbsp; @ param & nbsp; string & nbsp; $ url & nbsp; absolute url of the image & nbsp; * & nbsp; @ para what is the error in the code below?


/**
* Download remote images
* @ Param string $ absolute url of the url image
* @ Param string $ full path of the filepath file (including directories, excluding suffix names, such as/www/images/test ), this function automatically determines the image suffix based on the image url and http header information.
* @ Return mixed: an array describing image information is returned when downloading is successful. if downloading fails, false is returned.
*/
Function downloadImage ($ url, $ filepath ){
// Header information returned by the server
$ ResponseHeaders = array ();
// Original image name
$ Originalfilename = '';
// Suffix of the image
$ Ext = '';
$ Ch = curl_init ($ url );
// Set the value returned by curl_exec to include the Http header
Curl_setopt ($ ch, CURLOPT_HEADER, 1 );
// Set the value returned by curl_exec to include Http content
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
// Set the page after the capture jump (http 301,302)
Curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 1 );
// Set the maximum number of HTTP redirects
Curl_setopt ($ ch, CURLOPT_MAXREDIRS, 2 );

// Data returned by the server (including http header information and content)
$ Html = curl_exec ($ ch );
// Obtain the captured information
$ Httpinfo = curl_getinfo ($ ch );
Curl_close ($ ch );
If ($ html! = False ){
// Separate the header and body of the response. because the server may use 302 redirection, you need to separate the string into a substring of 2 + redirection times.
$ HttpArr = explode ("\ r \ n", $ html, 2 + $ httpinfo ['redirect _ count']);
// The second penultimate is the http header of the server's last response
$ Header = $ httpArr [count ($ httpArr)-2];
// The last part is the server's last response content
$ Body = $ httpArr [count ($ httpArr)-1];
$ Header. = "\ r \ n ";

// Obtain the header information of the last response
Preg_match_all ('/([a-z0-9-_] +): \ s * ([^ \ r \ n] +) \ r \ n/I', $ header, $ matches );
If (! Empty ($ matches) & count ($ matches) = 3 &&! Empty ($ matches [1]) &! Empty ($ matches [1]) {
For ($ I = 0; $ I <count ($ matches [1]); $ I ++ ){
If (array_key_exists ($ I, $ matches [2]) {
$ ResponseHeaders [$ matches [1] [$ I] = $ matches [2] [$ I];
}
}
}
// Get the image suffix
If (0 <preg_match ('{(? : [^ \/\] +) \. (Jpg | jpeg | gif | png | bmp) $} I ', $ url, $ matches )){
$ Originalfilename = $ matches [0];
$ Ext = $ matches [1];
} Else {
If (array_key_exists ('content-type', $ responseHeaders )){
If (0 <preg_match ('{image/(\ w +)} I', $ responseHeaders ['content-type'], $ extmatches )){
$ Ext = $ extmatches [1];
}
}

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.