![](/img/trans.png)
[英]How can I upload file in JavaScript and PHP without using FormData
[英]how can i upload a file using PHP
在此頁面上: http://developers.box.com/docs/
: http://developers.box.com/docs/
使用cURL上傳文件:
METHOD
POST /files/content
EXAMPLE REQUEST
curl https://api.box.com/2.0/files/content \
-H "Authorization: BoxAuth api_key=API_KEY&auth_token=AUTH_TOKEN" \
-F filename1=@FILE_NAME1 \
-F filename2=@FILE_NAME2 \
-F folder_id=FOLDER_ID
但是現在,我想用php上傳文件,我怎么能這樣做? 我的代碼:
<?php
$params = array();
$params['folder_id'] = '485272014';
$u_file = fopen("D:\code\php\bcs\test.data", "r");
$params['filename1'] = $u_file;
$params = json_encode($params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.box.com/2.0/files/content");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_UPLOAD, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: BoxAuth api_key=API_KEY&auth_token=TOKEN"));
$result = curl_exec($ch);
curl_close($ch);
print_r($result);
fclose($u_file);
?>
它不起作用,我使用: php -f test.php
運行腳本
我不認為POST表單數據可以接受使用創建的文件處理程序
fopen("D:\\code\\php\\bcs\\test.data", "r");
嘗試使用@代替訪問文件處理程序。 順便說一句,改變\\到/所以你不小心把一些角色作為轉義字符:
$u_file = "@D:/code/php/bcs/test.data";
您不應該對內容進行json_encode
,如果您的文件內容不是文本(例如,圖像/二進制文件),該怎么辦?
我認為這條線也給你帶來了問題。 用這個選項試了我的代碼,給我一個奇怪的“441必需長度”錯誤。 沒有這個選項我的代碼工作正常:
curl_setopt($ch, CURLOPT_UPLOAD, true);
最后,這是我的工作代碼:
<?php
public function upload_file()
{
$url = 'https://api.box.com/2.0/files/content';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_ENCODING, "UTF-8");
//this is my method to construct the Authorisation header
$header_details = array($this->default_authen_header());
curl_setopt($ch, CURLOPT_HTTPHEADER, $header_details);
$post_vars = array();
$post_vars['filename'] = "@C:/tmp_touch.txt";
$post_vars['folder_id'] = 0;
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_vars);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
?>
<?php
// ENTER YOUR DEVELOPER TOKEN
$token = "ekdfokeEdfdfkosdkoqwekof93kofsdfkosodSqd";
$url = "https://upload.box.com/api/2.0/files/content";
if (isset($_POST['btnUpload'])) {
$file_upload = $_FILES['file']['tmp_name'];
$json = json_encode(array(
'name' => $_FILES['file']['name'],
'parent' => array('id' => 0)
));
$fields = array(
'attributes' => $json,
'file'=>new CurlFile($_FILES['file']['tmp_name'],$_FILES['file']['type'],$_FILES['file']['name'])
);
try {
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer '.$token,
'Content-Type:multipart/form-data'
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$response = curl_exec($ch);
curl_close($ch);
} catch (Exception $e) {
$response = $e->getMessage();
}
print_r($response);
}
?>
<form method="post" name="frmUpload" enctype="multipart/form-data">
<tr>
<td>Upload</td>
<td align="center">:</td>
<td><input name="file" type="file" id="file"/></td>
</tr>
<tr>
<td> </td>
<td align="center"> </td>
<td><input name="btnUpload" type="submit" value="Upload" /></td>
</tr>
</form>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.