One month down in 2025: How are your resolutions coming along? Check out how to get back on track here.
Forum Discussion
jary_capstone
7 years agoHelpful | Level 5
Uploading files from a file server to DropBox
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:
- $app=new DropboxApp($consumerKey,$consumerSecret,$accessToken);
$dropbox=new Dropbox($app);
// Check if file was uploaded
if (isset($_FILES["file"])) {
// File to Upload
$file = $_FILES['file'];
// 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
Dropbox Staff
Hey jary_capstone!I’ve gone ahead and moved your post to the developer section of the Forum - perhaps some like-minded users will have some ideas to share on this with you.Cheers! :nerd: - Greg-DB
Dropbox Staff
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?
- jary_capstoneHelpful | Level 5
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.
- Greg-DB
Dropbox Staff
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.)`
About Discuss Dropbox Developer & API
Make connections with other developers807 PostsLatest Activity: 2 days ago
If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X or Facebook.
For more info on available support options for your Dropbox plan, see this article.
If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!