Upload a file into dropbox folder

依然范特西╮ 提交于 2020-06-28 04:59:16

问题


I have tried this to upload a file from a folder in my desktop to a folder in dropbox account.

but each time i have uploaded an empty file through this code.

How it is possible?

Below s my code:

     $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:


You don't seem to be adding the file content to the upload call anywhere, so an empty file is expected from your code.

You can find an example of using /2/files/upload with curl in PHP below. That uses CURLOPT_INFILE to add the file content itself.



<?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> should be replaced with the OAuth 2 access token.




回答2:


Upload a file into dropbox folder 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);


来源:https://stackoverflow.com/questions/42526998/upload-a-file-into-dropbox-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!