2010-07-11 60 views
3

我用cURL發送$ _FILES時遇到了一些麻煩 - 使用下面的代碼將文件傳輸好,但實際上我無法獲取文件的名稱和文件類型一旦$ _FILES到達他們的目的地,他們的類型被存儲爲「application/octet-stream」 - 我做錯了什麼!?

$count=count($_FILES['thefile']['tmp_name']); 

for($i=0;$i<$count;$i++) { 
    if(!empty($_FILES['thefile']['name'][$i])) { 
    $postargs[$i] = '@'.$_FILES['thefile']['tmp_name'][$i]; 
    } 
} 


$header = array("Content-type: multipart/form-data"); 
$ch = curl_init('http://localhost/curl/rec.php'); 
curl_setopt($ch,CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"); 
curl_setopt($ch,CURLOPT_RETURNTRANSFER, false); 
curl_setopt($ch,CURLOPT_ENCODING,""); 
curl_setopt($ch,CURLOPT_HTTPHEADER,$header); 
curl_setopt($ch,CURLOPT_FOLLOWLOCATION, TRUE); 
curl_setopt($ch,CURLOPT_POST,TRUE); 
curl_setopt($ch,CURLOPT_POSTFIELDS,$postargs); 
curl_exec($ch); 
curl_close($ch) 

回答

6

您應該建立$ postargs這樣,

$postargs = array(); 
foreach ($_FILES as $param => $file) { 
    $postargs[$param] = '@' . $file['tmp_name'] . ';filename=' . $file['name'] . ';type=' . $file['type']; 
}