Discuss Dropbox Developer & API
I've been adding some code to upload a file from my server to a specific folder in Dropbox but nothing successful. I'm using Yii2 advanced framework. (Php, javascript and html code in it).
These are the different options that I have used:
// File Path
$fileName = $file['name'];
$filePath = $file['tmp_name'];
try {
// Create Dropbox File from Path
$dropboxFile = new DropboxFile($filePath);
// Upload the file to Dropbox
$uploadedFile = $dropbox->upload($dropboxFile, "/" . $fileName, ['autorename' => true]);
// File Uploaded
echo $uploadedFile->getPathDisplay();
} catch (DropboxClientException $e) {
echo $e->getMessage();
}
}
2. This one creates the folder but does not save the file...
$inputData=Yii::$app->request->post();
$dropboxFile = new DropboxFile($filePath);
$folderName = $inputData['Model']['file];
$file = $dropbox->upload($dropboxFile, "/".$fileName, ['autorename' => true]);
$folderName = $inputData['Model']['file'];
$folder = $dropbox->createFolder($dropboxFile,"/".$folderName);
$path = 'https://www.dropbox.com/home/.../...' .$file->path_display;
$dropbox_path = array('dropbox_path'=>$path);
$inputData['Model']['dropbox_path'] = $path;
$model->load($inputData);
$model->save(false);
}
Any suggestion? Other code that I can implement to it?
Thanks in advance.
Based on this code, it looks like you've switched to using this other "Dropbox Uploader" library:
https://github.com/jakajancar/DropboxUploader
We do not recommend using this. From the its own readme:
"Its development was started before Dropbox released their API, and to work, it scrapes their website. So you can and probably should use their API now as it is much more stable."
I recommend using a third party library that actually uses the API, like you were before, or calling the API directly.
Lusil
Community Moderator @ Dropbox
dropbox.com/support
Did this post help you? If so, please give it a Like below.
Still stuck? Ask me a question!
Tips & Tricks Find new ways to stay in flow or share your tips on how you work smarter with Dropbox.
From the code you provided, it looks like you're using this third party library:
https://github.com/kunalvarma05/dropbox-php-sdk
We can't provide support for third party libaries, as we didn't make them, but we'll be happy to help with any issues you're having with the Dropbox API itself.
Whenever you make a Dropbox API call, you should receive a response indicating if the call succeeded or failed. It sounds like you're having trouble uploading to Dropbox. What does the 'upload' method return or throw?
I can change the code from third party to the official Dropbox Api.
What I want to do is to select the file that I want to upload and click the Save/Upload button to save it in Dropbox in a specific folder directory.
The upload method that I have right now is not throwing any error. It works as if it's uploading the file but it returns to the same page without any warning or flash message.
If the method is not throwing an error, what is it returning? (I.e., `$uploadedFile` in your code #1, or `$file` in your code #2.)`
$uploadedFile in code #1.
It seems the variable is not taking the file that I selected so the variable is coming empty.
I'm not sure I understand what you mean when you say "the variable is not taking the file that I selected".
Do you mean $uploadedFile is null after the upload method runs? It sounds like the method didn't successfully run. You may need to debug your code more, or double check if you're getting any other output or error.
You actually got to the point... the method did not succesfully run.
I change the code to a different one and now it's running but it's not saving in the desire folder: using DropboxUploader
<?php
if ($_POST) {
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!');
// Enter your Dropbox account credentials here
$uploader = new DropboxUploader('email', 'password');
$uploader->upload($tmpFile, $_POST['dest']);
echo '<span style="color: green;font-weight:bold;margin-left:393px;">File successfully uploaded to my Dropbox!</span>';
} catch(Exception $e) {
echo '<span style="color: red;font-weight:bold;margin-left:393px;">Error: ' . htmlspecialchars($e->getMessage()) . '</span>';
}
// Clean up
if (isset($tmpFile) && file_exists($tmpFile))
unlink($tmpFile);
if (isset($tmpDir) && file_exists($tmpDir))
rmdir($tmpDir);
}
?>
Based on this code, it looks like you've switched to using this other "Dropbox Uploader" library:
https://github.com/jakajancar/DropboxUploader
We do not recommend using this. From the its own readme:
"Its development was started before Dropbox released their API, and to work, it scrapes their website. So you can and probably should use their API now as it is much more stable."
I recommend using a third party library that actually uses the API, like you were before, or calling the API directly.
I'll try my best. If it start to give me errors or problems for upload a file then I will recurr here to the forum and ask about it.
Thanks
Hi there!
If you need more help you can view your support options (expected response time for a ticket is 24 hours), or contact us on Twitter or Facebook.
For more info on available support options, see this article.
If you found the answer to your question, please 'like' the post to say thanks to the user!