2012-08-14 91 views
0

我上傳文件到遠程服務器。代碼:PHP cURL文件上傳附加信息到文件

$ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, ZDURL.$url); 
    curl_setopt($ch, CURLOPT_USERPWD, ZDUSER."/token:".ZDAPIKEY); 

    $params = array('file_name' => '@'.$temp_file); 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/plain")); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

    curl_setopt($ch, CURLINFO_HEADER_OUT, 0); 
    curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0); 
    curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0); 
    $result = curl_exec($ch); 
    $error = curl_error($ch); 
    curl_close($ch); 

但在上傳文件的頂部顯示的信息:

------------------------------cfbe90a606af 
Content-Disposition: form-data; name="file_name"; filename="C:\Program Files\xampp\tmp\phpF576.tmp" 
Content-Type: application/octet-stream 

圖像有由於這除了文字

+0

'內容類型:text/plain'是沒有意義在這裏給你所談論的圖像(和一般將數組傳遞給'CURLOPT_POSTFIELDS'時)。你想做什麼,模仿表單提交,發佈原始圖像或什麼? – DaveRandom 2012-08-14 08:56:15

+0

發佈原始圖像。我使用這個示例http://dtbaker.com.au/random-bits/uploading-a-file-using-curl-in-php.html – 2012-08-14 09:56:41

回答

0

這聽起來像你將數據發送到URL格式錯誤預期只是請求正文中的文件,而不是表單提交。達到此目的的最佳方式是CURLOPT_INFILE。試試這個代碼:

試試這個

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, ZDURL.$url); 
curl_setopt($ch, CURLOPT_USERPWD, ZDUSER."/token:".ZDAPIKEY); 

// You need to detect the correct content type for the file you are sending. 
// Although based on the current problem you have, it looks like the server 
// ignores this anyway 
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: image/jpeg")); 

// Send the raw file in the body with no other data 
$fp = fopen($temp_file, 'r'); 
curl_setopt($ch, CURLOPT_INFILE, $fp); 

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLINFO_HEADER_OUT, 0); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 

$result = curl_exec($ch); 
$error = curl_error($ch); 

curl_close($ch); 
fclose($fp); 
+0

謝謝!工作代碼: – 2012-08-14 11:05:59