Skip to content

Instantly share code, notes, and snippets.

@stevenwoods
Last active November 18, 2015 02:47
Show Gist options
  • Save stevenwoods/6131e92ac4d5958017a5 to your computer and use it in GitHub Desktop.
Save stevenwoods/6131e92ac4d5958017a5 to your computer and use it in GitHub Desktop.
Dropbox Upload File
Download the SDK and follow the instructions on generating the parameters for auth.json here:
https://www.dropbox.com/developers-v1/core/start/php
{"key": "key","secret": "secrety","access_token":"access_token"}
<?php
require_once __DIR__."/lib/Dropbox/strict.php";
require_once __DIR__.'/lib/Dropbox/autoload.php';
use \Dropbox as dbx;
list($accessToken, $host) = dbx\AuthInfo::loadFromJsonFile('auth.json');
$locale = null;
$client = new dbx\Client($accessToken, "upload", $locale, $host);
$sourcePath = 'test.txt';
$dropboxPath = '/test.txt';
$pathError = dbx\Path::findErrorNonRoot($dropboxPath);
if ($pathError !== null) {
fwrite(STDERR, "Invalid <dropbox-path>: $pathError\n");
die;
}
$size = null;
if (\stream_is_local($sourcePath)) {
$size = \filesize($sourcePath);
}
$fp = fopen($sourcePath, "rb");
$metadata = $client->uploadFile($dropboxPath, dbx\WriteMode::add(), $fp, $size);
fclose($fp);
print_r($metadata);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment