2013-05-10 118 views
0

我已經設置了一個HTML表單來選擇一個文件並將其提交給PHP腳本,該腳本將上傳它。我不能使用move_uploaded_files(),因爲Box的API要求我爲Authorization: access_token添加HTTP標題。我所做的是使用cURL庫設置我自己的POST方法。使用PHP上傳文件

我遇到的問題是正確設置文件名,因爲它需要文件的完整路徑。我無法從HTML表單獲取文件的完整路徑,並使用$_FILES['filename']['tmp_name']上傳我不想要的.tmp文件。有誰知道這個問題的解決方案?非常感謝!

我的代碼:

public function upload_file($file) { 
     $url = 'https://api.box.com/2.0/files/content'; 
     $params = [ 
      'filename' => '@'.$file['tmp_name'], 
      'folder_id' => '0' 
     ]; 
     $header = "Authorization: Bearer ".$this->access_token; 
     $data = $this->post($url, $params, $header); 
    } 

public function post($url, $params, $header='') { 
     $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_POST, true); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, $params); 
     if(!empty($header)) { 
      curl_setopt($ch, CURLOPT_HTTPHEADER, array($header)); 
     } 
     $data = curl_exec($ch); 
     curl_close($ch); 

     return $data; 
    } 
+0

你能提供你的代碼? – seanrose 2013-05-11 21:12:52

+0

當然,我只是將我的代碼添加到原始帖子中。對不起,原來不包括它。 – user2066880 2013-05-13 16:01:10

回答

0

建議你做以下之一:

  1. 使用move_uploaded_files上傳文件到某個目錄,並使用該位置發送文件使用curl框。成功上傳後,您可以從該目錄中刪除該文件。
  2. 而不是通過PHP上傳文件,可以上傳到客戶端使用CORS http://developers.blog.box.com/2013/05/13/uploading-files-with-cors/

另一個問題,我在我的腦海中你是如何保持你刷新的access_token?

  • 維沙爾
0

我什麼維沙爾在點數人提出同意。

我已經寫了PHP SDK爲V2

就包括API類和啓動類:

<?php 
    include('library/BoxAPI.class.php'); 

    $client_id = 'CLIENT ID'; 
    $client_secret = 'CLIENT SECRET'; 
    $redirect_uri = 'REDIRECT URL'; 
    $box = new Box_API($client_id, $client_secret, $redirect_uri); 

    if(!$box->load_token()){ 
     if(isset($_GET['code'])){ 
      $token = $box->get_token($_GET['code'], true); 
      if($box->write_token($token, 'file')){ 
       $box->load_token(); 
      } 
     } else { 
      $box->get_code(); 
     } 
    } 
    // Upload file 
    $box->put_file('RELATIVE FILE URL', '0')); 
?> 

看一看here 下載:BoxPHPAPI