8

I have a form that allows a user to fill in several aspects and then choose a file to upload.

When the form is submitted, I want write some code that saves the file to a dropbox account and gets access to a direct download link and places this in a database I am hosting.

If anyone has done this, is there a specific section of the API to look at? Or any examples?

I can't seem to find this in the API.

Thanks.

sark9012
  • 5,485
  • 18
  • 61
  • 99
  • did you manage to solve your problem with any of the answers below? If so, please take a moment to upvote and accept the answer you find best. :) – tftd Jan 14 '14 at 16:52

2 Answers2

17

From what I see in the API it is possible to do this. You need to download the Dropbox Core API. Inside the zip file, you will find an example folder with example code for authentication, upload, download, direct-link and so on. Just see the direct-link.php and change it to your needs. Here is a tested working example of uploading a file and generating a direct link for download:

<?php

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

use \Dropbox as dbx;

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

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

$authorizeUrl = $webAuth->start();
echo "1. Go to: " . $authorizeUrl . "<br>";
echo "2. Click \"Allow\" (you might have to log in first).<br>";
echo "3. Copy the authorization code and insert it into $authCode.<br>";

$authCode = trim('DjsR-iGv4PAAAAAAAAAAAbn9snrWyk9Sqrr2vsdAOm0');

list($accessToken, $dropboxUserId) = $webAuth->finish($authCode);
echo "Access Token: " . $accessToken . "<br>";

$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);
// adding ?dl=1 to the link will force the file to be downloaded by the client.
$dw_link = $link[0]."?dl=1";

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

?>

I made this really fast just to get it working. Eventually you may need to tweak it a bit so it will suite your needs.

tftd
  • 16,203
  • 11
  • 62
  • 106
  • 1
    how/where to get $authCode? – alamnaryab Feb 18 '15 at 05:04
  • 1
    As far as I remember, that was generated when you go through the authorization process. The example I've posted combines the `examples/authorize.php`, `examples/upload-file.php` and `examples/direct-link.php`. You should go through them to better understand how the API works. – tftd Feb 21 '15 at 16:49
  • So After I clicked the the link, it gave me this message saying "Enter this code into myApps to finish the process. Where do i exactly put this if im working from a website? – Carlitos Sep 15 '16 at 03:24
  • I don't remember any more. Probably when you go to `$authorizeUrl` in your browser, it will give you an authentication code. You should copy that code in `$authCode` and open the url to the php script. – tftd Sep 15 '16 at 22:20
1

There is section in the Core API manual, see this link. So you can use the upload part like this:

$f = file_get_contents('data.txt');
$result = $dbxClient->uploadFile("/data.txt", dbx\WriteMode::add(), $f);

echo 'file uploaded';
aksu
  • 5,221
  • 5
  • 24
  • 39