1

I am newbie to Dropbox API. I have completed the code which upload the file (on my website host) to my Dropbox with a given token. The process runs successfully. I want to design a page which allows the user select a file (from his/her local machine) and upload directly to my dropbox.

I have an idea that the controller will upload the file to host first then upload to dropbox. However this idea sucks as it takes more time and bandwidth to complete. And I have to delete the file on host after uploading.

This is the code which works on my host:

<?php

require_once "dropbox-sdk/lib/Dropbox/autoload.php";

use \Dropbox as dbx;

$dropbox_config = array(
    'key'    => 'my key',
    'secret' => 'my secret key'
);

$appInfo = dbx\AppInfo::loadFromJson($dropbox_config);
$webAuth = new dbx\WebAuthNoRedirect($appInfo, "PHP-Example/1.0");


$accessToken = 'my token code is given here';
$dbxClient = new dbx\Client($accessToken, "PHP-Example/1.0");

// Uploading the file
$f = fopen("working-draft.txt", "rb");
$result = $dbxClient->uploadFile("/working-draft.txt", dbx\WriteMode::add(), $f);
fclose($f);
//print_r($result);

// Get file info
$file = $dbxClient->getMetadata('/working-draft.txt');

// sending the direct link:
$dropboxPath = $file['path'];
$pathError = dbx\Path::findError($dropboxPath);
if ($pathError !== null) {
    fwrite(STDERR, "Invalid <dropbox-path>: $pathError\n");
    die;
}

// The $link is an array!
$link = $dbxClient->createTemporaryDirectLink($dropboxPath);
$dw_link = $link[0]."?dl=1";

echo "Download link: ".$dw_link."<br>";

?>

And I am using Codeigniter

2

1 Answer 1

0

I have an idea that the controller will upload the file to host first then upload to dropbox. However this idea sucks as it takes more time and bandwidth to complete.

Unfortunately, this is the only secure way to do this. Adding a file to your Dropbox requires knowing an OAuth access token for your account, and you can't allow others to know that token. (It would give them control of the account.) So the token needs to be kept secret, on your server. That means the file needs to be uploaded to your server and transferred to Dropbox from there.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.