2015-07-10 118 views
0

我的表單將所有表單數據發佈到一個名爲upload.php的文件。

問題是:是否有可能重新發布的multipart/form-data的,包括$ _FILES我的REST框架SLIM這樣的:

$headers = array('Content-type: multipart/form-data','Authorization: '.$api_key,); 

$curl_post_data = array('employee_id' => $employee_id,'files' => $_FILES); 
$curl = curl_init('http://[...mydomain...]/v1/uploadEquipmentDocument'); 

curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($curl, CURLOPT_POST, true); 
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data); 
curl_setopt($curl, CURLOPT_USERPWD, $api_key); 

$curl_response = curl_exec($curl); 

謝謝..

回答

1

如果你想要使用cUrl上傳文件,您不能只傳遞您在$ _FILES數組中獲得的文件名(temp/real),因爲這是一個封裝器,當您想要使用上傳到你的服務器。

你有兩個選擇:

選項1:
使用@告訴cUrl作者:這不只是一個變量,這是你要上傳的文件。在這種情況下,php will post the file itself(而不僅僅是字符串,它是文件的名稱):(我假設你原來的形式有<input type="file" name="uploaded_file1" />爲了使用原來的輸入名字這個工作)

$curl_post_data['file1'] = "@" . $_FILES['uploaded_file1']['tmp_name']; 

選項數2:
如果你正在使用PHP> = 5.5,你可以使用新的CURLFile object,它給你的排序相同的結果,並有點不同的做法:

// Create a CURLFile object 
$cfile = new CURLFile($_FILES['uploaded_file1']['tmp_name'], $_FILES['uploaded_file1']['type'], $_FILES['uploaded_file1']['name']); 

// Assign POST data 
$curl_post_data = array('file1' => $cfile); 
curl_setopt($curl, CURLOPT_POST, 1); 
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data); 

// Execute the handle 
curl_exec($curl);