PHP multi-threaded download remote multiple file functions

Source: Internet
Author: User
This article introduces, with PHP implementation of a multi-threaded download remote multiple files function, the need for a friend reference.

The following code, implementation: Multi-threaded Download remote multiple files, as follows:

      $V) {if (!empty ($v) && preg_match ("~^http~i", $v)) {$nurl [$k] = Trim (Str_replace (","%20 ", $v)); $curl [$k] = cur L_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,); 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) and while ($active &am p;& $MRC = = CURLM_OK) {if (Curl_multi_select ($handle)! =-1) {does {$MRC = Curl_multi_exec ($handle, $active);} while ($m rc = = 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;}? >
  • 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.