Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #1 from Mic92/master
add a remoteName parameter to upload()
  • Loading branch information
jakajancar committed May 31, 2012
2 parents 650a555 + f5ace66 commit c8f34ab
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 24 deletions.
24 changes: 16 additions & 8 deletions DropboxUploader.php
Expand Up @@ -62,21 +62,29 @@ public function setCaCertificateDir($dir)
$this->caCertSourceType = self::CACERT_SOURCE_DIR;
$this->caCertSource = $dir;
}
public function upload($filename, $remoteDir='/') {
if (!file_exists($filename) or !is_file($filename) or !is_readable($filename))
throw new Exception("File '$filename' does not exist or is not readable.");

public function upload($source, $remoteDir='/', $remoteName=null) {
if (!file_exists($source) or !is_file($source) or !is_readable($source))
throw new Exception("File '$source' does not exist or is not readable.");

if (!is_string($remoteDir))
throw new Exception("Remote directory must be a string, is ".gettype($remoteDir)." instead.");
throw new Exception("Remote directory must be a string, is ".gettype($remoteDir)." instead.");

if (is_null($remoteName)) {
$remoteName = $source;
} else if (!is_string($remoteName)) {
throw new Exception("Remote filename must be a string, is ".gettype($remoteDir)." instead.");
}

if (!$this->loggedIn)
$this->login();

$data = $this->request('https://www.dropbox.com/home');
$token = $this->extractToken($data, 'https://dl-web.dropbox.com/upload');

$data = $this->request('https://dl-web.dropbox.com/upload', true, array('plain'=>'yes', 'file'=>'@'.$filename, 'dest'=>$remoteDir, 't'=>$token));


$postdata = array('plain'=>'yes', 'file'=>'@'.$source.';filename='.$remoteName, 'dest'=>$remoteDir, 't'=>$token);
$data = $this->request('https://dl-web.dropbox.com/upload', true, $postdata);
if (strpos($data, 'HTTP/1.1 302 FOUND') === false)
throw new Exception('Upload failed!');
}
Expand Down
17 changes: 1 addition & 16 deletions example.php
Expand Up @@ -11,36 +11,21 @@
require 'DropboxUploader.php';

try {
// Rename uploaded file to reflect original name
if ($_FILES['file']['error'] !== UPLOAD_ERR_OK)
throw new Exception('File was not successfully uploaded from your computer.');

$tmpDir = uniqid('/tmp/DropboxUploader-');
if (!mkdir($tmpDir))
throw new Exception('Cannot create temporary directory!');

if ($_FILES['file']['name'] === "")
throw new Exception('File name not supplied by the browser.');

$tmpFile = $tmpDir.'/'.str_replace("/\0", '_', $_FILES['file']['name']);
if (!move_uploaded_file($_FILES['file']['tmp_name'], $tmpFile))
throw new Exception('Cannot rename uploaded file!');

// Upload
$uploader = new DropboxUploader($_POST['email'], $_POST['password']);
$uploader->upload($tmpFile, $_POST['dest']);
$uploader->upload($_FILES['file']['tmp_name'], $_POST['dest'], $_FILES['file']['name']);

echo '<span style="color: green">File successfully uploaded to your Dropbox!</span>';
} catch(Exception $e) {
echo '<span style="color: red">Error: ' . htmlspecialchars($e->getMessage()) . '</span>';
}

// Clean up
if (isset($tmpFile) && file_exists($tmpFile))
unlink($tmpFile);

if (isset($tmpDir) && file_exists($tmpDir))
rmdir($tmpDir);
}
?>
<form method="POST" enctype="multipart/form-data">
Expand Down

0 comments on commit c8f34ab

Please sign in to comment.