Reshma
Reshma

Reputation: 199

Upload a file into dropbox folder

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

Upvotes: 1

Views: 2600

Answers (2)

Md.Aminul Islam
Md.Aminul Islam

Reputation: 11

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

Upvotes: 1

Greg
Greg

Reputation: 16930

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.

Upvotes: 1

Related Questions