2017-03-01 90 views
1

我已經嘗試過將文件從桌面上的文件夾上傳到保管箱帳戶中的文件夾。將文件上傳到保管箱文件夾

但每次我通過這段代碼上傳了一個空文件。

怎麼可能?

下面是我的代碼:

 $ch = curl_init(); 
    $TOKEN = "asasasa";//token here 
    $url = 'https://content.dropboxapi.com/2/files/upload'; 
    curl_setopt($ch,CURLOPT_URL,$url); 
    curl_setopt($ch,CURLOPT_POST, 1);    
    curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); 
    $headers = array(); 
    $headers[] = 'Accept: application/json'; 
    $headers[] = 'Content-Type: multipart/form-data'; 
    $headers[] = 'Dropbox-API-Arg:{"path":"/home/new/ofd","mode":{".tag":"add"},"autorename":false,"mute":false}'; 
    $headers[] = "Authorization: Bearer ".$TOKEN; 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);  
    $response = curl_exec($ch); 
    $droplist = json_decode($response,true); 

回答

1

你似乎沒有在任何地方添加文件內容上傳呼叫,所以一個空文件從你的代碼的預期。

你可以在下面的PHP中找到一個在curl中使用/ 2/files/upload的例子。這使用CURLOPT_INFILE本身添加文件內容。



<?php 

$path = 'test_php_upload.txt'; 
$fp = fopen($path, 'rb'); 
$size = filesize($path); 

$cheaders = array('Authorization: Bearer <ACCESS_TOKEN>', 
        'Content-Type: application/octet-stream', 
        'Dropbox-API-Arg: {"path":"/test/'.$path.'", "mode":"add"}'); 

$ch = curl_init('https://content.dropboxapi.com/2/files/upload'); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $cheaders); 
curl_setopt($ch, CURLOPT_PUT, true); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); 
curl_setopt($ch, CURLOPT_INFILE, $fp); 
curl_setopt($ch, CURLOPT_INFILESIZE, $size); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$response = curl_exec($ch); 

echo $response; 
curl_close($ch); 
fclose($fp); 

?> 

<ACCESS_TOKEN>應該使用OAuth 2訪問令牌代替。

0

上傳文件到Dropbox的文件夾的Dropbox API V2 $dropbox_api_url = ' https://content.dropboxapi.com/2/files/upload '; //dropbox api url $token = 'Access token value put here'; // should be replaced with the OAuth 2 access token $headers = array('Authorization: Bearer '. $token, 'Content-Type: application/octet-stream', 'Dropbox-API-Arg: '. json_encode( array( "path"=> '/'. basename($filename), "mode" => "add", "autorename" => true, "mute" => false ) ) ); $ch = curl_init($dropbox_api_url); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POST, true); $path = $filename; $fp = fopen($path, 'rb'); $filesize = filesize($path); curl_setopt($ch, CURLOPT_POSTFIELDS, fread($fp, $filesize)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //For display value as array object starts here echo ""; print_r(json_decode($response)); //For display value as array object ends here echo($response.'
'); echo($http_code.'
'); curl_close($ch);