将文件上传到保管箱文件夹

Upload a file into dropbox folder

我试过将文件从我桌面上的文件夹上传到 Dropbox 帐户中的文件夹。

但是每次我通过这个代码上传一个空文件。

怎么可能?

下面是我的代码:

     $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);

您似乎没有在任何地方将文件内容添加到上传调用,因此您的代码应该是一个空文件。

您可以在下面的 PHP 中找到将 /2/files/upload 与 curl 结合使用的示例。使用 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 访问令牌。

将文件上传到 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 "<pre>";
        print_r(json_decode($response));
        //For display value as array object ends here
        echo($response.'<br/>');
        echo($http_code.'<br/>');
        curl_close($ch);