$dir = 'dist302'; $files = read_dir($dir); function read_dir($dir){ $files=array(); $dir_list=scandir($dir); foreach($dir_list as $file){ if($file!='..' && $file!='.'){ if(is_dir($dir.'/'.$file)){ $files[]=read_dir($dir.'/'.$file); }else{ $files[]=$file; } } } return $files; } doUpload($files,$dir); function doUpload($array = array(),$dir){ $wait_usec = 0;//intval($wait_usec); $data = array(); $handle = array(); $running = 0; $mh = curl_multi_init(); // multi curl handler $i = 0; $url = 'http://www.baidu.com'; foreach($array as $file) { $filename =realpath($dir.'/'.$file); $minetype = 'image/jpeg'; $curl_file = curl_file_create($filename,$minetype); $post_data = array( 'fileName' => $file, 'user_file'=>$curl_file ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return don't print curl_setopt($ch, CURLOPT_TIMEOUT, 30); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)'); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // 302 redirect curl_setopt($ch, CURLOPT_MAXREDIRS, 7); //設置post方式提交 curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); curl_setopt($ch, CURLOPT_TIMEOUT, 5);//設置超時時間 curl_multi_add_handle($mh, $ch); // 把 curl resource 放進 multi curl handler 裏 $handle[$i++] = $ch; } /* 執行 */ do { curl_multi_exec($mh, $running); if ($wait_usec > 0) /* 每一個 connect 要間隔多久 */ usleep($wait_usec); // 250000 = 0.25 sec } while ($running > 0); /* 讀取資料 */ foreach($handle as $i => $ch) { $content = curl_multi_getcontent($ch); $data[$i] = (curl_errno($ch) == 0) ? $content : false; } /* 移除 handle*/ foreach($handle as $ch) { curl_multi_remove_handle($mh, $ch); } curl_multi_close($mh); foreach($data as $key => $json){ $info = json_decode($json, true); print_r($info['data']); if(empty($info['errcode'])){ $arr = explode(".",$info['data']['picName']); $msg = $arr[0].' '.$info['data']['picUrl'] . PHP_EOL; file_put_contents('success.txt', $msg, FILE_APPEND); } else{ $msg = $$json. PHP_EOL; file_put_contents('error.txt', $msg, FILE_APPEND); } } //return $data; }