Parse multiple php threads to download multiple remote files. Copy the code as follows: functionremote ($ urls, $ name, $ path, $ dir. images) {if (! Is_array ($ urls) orcount ($ urls) 0) {returnfalse;} dmkdir ($ dir); $ curl $
The code is as follows:
Function remote ($ urls, $ name = '', $ path ='', $ dir = './images /'){
If (! Is_array ($ urls) or count ($ urls) = 0 ){
Return false;
}
Dmkdir ($ dir );
$ Curl = $ text = array ();
Foreach ($ urls as $ k => $ v ){
If (! Empty ($ v) & preg_match ("~ ^ Http ~ I ", $ v )){
$ Nurl [$ k] = trim (str_replace (''," % 20 ", $ v ));
$ Curl [$ k] = curl_init ($ nurl [$ k]);
Curl_setopt ($ curl [$ k], CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
Curl_setopt ($ curl [$ k], CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ curl [$ k], CURLOPT_HEADER, 0 );
Curl_setopt ($ curl [$ k], CURLOPT_CONNECTTIMEOUT, 20 );
If (! Isset ($ handle )){
$ Handle = curl_multi_init ();
}
Curl_multi_add_handle ($ handle, $ curl [$ k]);
}
Continue;
}
$ Active = null;
Do {
$ Mrc = @ curl_multi_exec ($ handle, $ active );
} While ($ mrc = CURLM_CALL_MULTI_PERFORM );
While ($ active & $ mrc = CURLM_ OK ){
If (curl_multi_select ($ handle )! =-1 ){
Do {
$ Mrc = curl_multi_exec ($ handle, $ active );
} While ($ mrc = CURLM_CALL_MULTI_PERFORM );
}
}
Foreach ($ curl as $ k => $ v ){
If (curl_error ($ curl [$ k]) = ""){
If ($ k = 0 ){
$ Fname [$ k] = strtolower ($ name. '.'. pathinfo ($ urls [$ k], PATHINFO_EXTENSION ));
} Else {
$ Fname [$ k] = strtolower ($ name. '_'. $ k. '.'. pathinfo ($ urls [$ k], PATHINFO_EXTENSION ));
}
$ Text [$ k] = (string) curl_multi_getcontent ($ curl [$ k]);
$ Filedir [$ k] = $ dir. '/'. $ fname [$ k];
If (file_put_contents ($ filedir [$ k], $ text [$ k]) {
$ Filepath [$ k] = $ path. $ fname [$ k];
}
}
Curl_multi_remove_handle ($ handle, $ curl [$ k]);
Curl_close ($ curl [$ k]);
}
Curl_multi_close ($ handle );
Return $ filepath;
}
The http://www.bkjia.com/PHPjc/327835.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/327835.htmlTechArticle code is as follows: function remote ($ urls, $ name = '', $ path ='', $ dir = './images/') {if (! Is_array ($ urls) or count ($ urls) = 0) {return false;} dmkdir ($ dir); $ curl = $...