Dropbox File Upload api Using PHP Oauth
Dropbox file upload api using php Oauth is very interesting and pretty simple. Now these days its very important to upload users files to the storage spaces like Dropbox,Google drive etc.The main advantage on this system is we don’t need to lost our server space for storing those files and some graphics kind of things directly on your Dropbox!.
That is the Dropbox file upload api using PHP for you. First of all you should have an account on the Dropbox then you have to create an app with Dropbox. Create your Dropbox app here. Once you created the app you will get two keys with that you app will synch from your server to Dropbox server.
When you get dropbox keys now you have to download the configuration file for the Dropbox file upload using php Oauth api from bottom of the article. After downloading these files you have to create a folder on the server with named as “dropbox” and upload these two files to that folder. Then create a folder inside the dropbox folder and named as tokens. I will explain the usage of this folder later.
Upto this you have done our half job is done. Now we have required a UI for the user to upload the files. So you have to create a file with dropboxupload.php and have the following codes.
<?php
require_once("dropbox/DropboxClient.php");
$dropbox = new DropboxClient(array(
'app_key' => "APP_KEY",
'app_secret' => "APP_SECRET",
'app_full_access' => false,
),'en');
handle_dropbox_auth($dropbox); // see below
// if there is no upload, show the form
if(empty($_FILES['the_upload'])) {
?>
<form enctype="multipart/form-data" method="POST" action="">
<p>
<label for="file">Upload File</label>
<input type="file" name="the_upload" />
</p>
<p><input type="submit" name="submit-btn" value="Upload!"></p>
</form>
<?php
}else{
$upload_name = $_FILES["the_upload"]["name"];
$result = $dropbox->UploadFile($_FILES["the_upload"]["tmp_name"], $upload_name);
print_r($result);
}
// store_token, load_token, delete_token are SAMPLE functions! please replace with your own!
function store_token($token, $name)
{
file_put_contents("dropbox/tokens/$name.token", serialize($token));
}
function load_token($name)
{
if(!file_exists("dropbox/tokens/$name.token")) return null;
return @unserialize(@file_get_contents("dropbox/tokens/$name.token"));
}
function delete_token($name)
{
@unlink("dropbox/tokens/$name.token");
}
function handle_dropbox_auth($dropbox)
{
// first try to load existing access token
$access_token = load_token("access");
if(!empty($access_token)) {
$dropbox->SetAccessToken($access_token);
}
elseif(!empty($_GET['auth_callback'])) // are we coming from dropbox's oauth page?
{
// then load our previosly created request token
$request_token = load_token($_GET['oauth_token']);
if(empty($request_token)) die('Request token not found!');
// get & store access token, the request token is not needed anymore
$access_token = $dropbox->GetAccessToken($request_token);
store_token($access_token, "access");
delete_token($_GET['oauth_token']);
}
// checks if access token is required
if(!$dropbox->IsAuthorized())
{
// redirect user to dropbox oauth page
$return_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME']."?auth_callback=1";
$auth_url = $dropbox->BuildAuthorizeUrl($return_url);
$request_token = $dropbox->GetRequestToken();
store_token($request_token, $request_token['t']);
die("Authentication required".$auth_url);
}
}
?>
In the above file you have seen a folder with name as token inside that folder the token details are storing .The Dropbox file upload api with php Oauth is based on authentication system but its not practical for before every uploads authenticating the accounts . So once you authenticating with the Dropbox it will never ask for authentication until you remove token file or modify you apps.
Make sure your form submitting to the different target file then you should include the config file and all the other function from the above page for authentication, like the page contains below code required authentications.
$dropbox->UploadFile($_FILES["the_upload"]["tmp_name"], $upload_name);
For more details about the dropbox core api are you can find here. Another thing is using dropbox api , allow a maximum size of 150Mb for uploads.
Note:Possible errors occur while integration are.
“Request token not found!” This means the token file not found or the path to token folder is wrong.
Another situation is when your target form is a different file you forgot to include the config files on that page.It will show some error like get_file_contents(path..) cannot read files.
Tips:While facing such errors you can check the error_log inside the dropbox folder for other errors. Hope this article will helps you .. Good Luck 🙂 🙂 🙂
43 thoughts on “Dropbox File Upload api Using PHP Oauth”
I am totally lost on the token issue described in the instructions and in the questions.
(1) What does this mean “// store_token, load_token, delete_token are SAMPLE functions! please replace with your own!”
(2) What is “store_token($token, $name)” – is this a file we need to create?
(3) What is “function load_token($name)”
(4) What is “function delete_token($name)”
(5) What files are to be placed in the “dropbox/tokens/” folder?
I ask these questions because I have attempted to implement this and get the following error: “Authentication requiredhttps://www.dropbox.com/1/oauth/authorize?oauth_token=…..”
your questions from 1- 4 are not need to do any thing those are library function and do not need to change anything except token values. set the token folder path correctly .
first time when you try to upload the files you have to authorize it your self,
How to authorize the first upload can be found in my older comments.
Once you authorize the APP with first upload by yourself, it will generate a token file inside your token folder . so next time onwards it took from there.
Hope its clear
Your tutorial is great. But is it possible to upload files to different dropbox account using dropbox email and password?
You can’t upload to a another drop box account only with email and password.
You need to create an APP and set the proper permission in the APP then only you will be able to upload files to those account.
Hope its make sense.
so by creating an app at Dropbox. Will it let me upload to different accounts? because I am bit confused with this app thing. Because app is created using a single account. Then how it will access different account?
No dude it different , you can create app and only upload files to that account not to other account.
means which account you’re creating APP, you only able to upload files to that account.
Hi , Greetings,
Thanks for the code, i am trying to upload more than 2MB files i am getting this error
A PHP Error was encountered
Severity: Warning
Message: file_get_contents(): Filename cannot be empty
Filename: DropboxClient.php
Line Number: 328
Please help to rectify this error , Thanks in advance
Its seems like you tried with invalid file.
Other possibility is CURL lib is not enabled in your server. try to enable that.
The link is not working
The One to Download the code
I got a similar code from other website but i have aquestion , i generate a token for one user and save it in the file but if a different user comes how will he be able to upload the file on his dropbox account , because in the code apart from token there is nothing else mentioned no email id or password from user.
I think you understood the things wrongly .
Others users uploading the files to your dropbox not their own.
So once you authenticate and upload a test file token will created then APP get verified so users can upload files to your dropbox from webpages directly with out loin to Dropbox.
By the way corrected download link try it.
Oh thanks friend due to server migration all package path changed, I didn’t noticed yet.
thanks for help me to figure it out.
Did you getting these comment responses as email ? new server email is not sending 🙁
I think there’s something amiss… isn’t chunked_upload supposed to be the feature that allows uploads beyond 150mb?
Yes, You can upload more than 150mb using chucked upload.
Hi,
I am getting error like this
Authentication required
https://www.dropbox.com/1/oauth/authorize?oauth_token=2XkpQBHBASJetfn5&oauth_callback=http%3A%2F%2Flocalhost%2Fanto%2Frough%2Fex%2Fdropbox%2Findex.php%3Fauth_callback%3D1
can you plz help me to get out of this issue.
Regards,
Anto S
You’re trying the APP from “localhost” most of the case its not works.
Bcoz the Dropbox app creation required domain verification.
So try it in a domain name will work fine..
Hi,
Thanks for your reply.
After some try it was working fine in the late night, but unfortunately I got a mail from dropbox saying that Your production key request was declined for the following reason etc.. since then my app is not working.and getting error as below again,
Authentication requiredhttps://www.dropbox.com/1/oauth/authorize?oauth_token=fpO7qCs7I0CRzMcB&oauth_callback=http%3A%2F%2Flocalhost%2Fanto%2Frough%2Fex%2Fdropbox%2Findex.php%3Fauth_callback%3D1
I have few question now,
1. If the issue with local host it should not have worked in the late night also.
2. If it is working before, it should not throw the message like authentication required now,
Whats the issue now?
Regards,
Anto S
I thinks it due to localhost most of the case Oauth API will not support localhost (some of them works like yahoo) but mostly not work.
So the only reason is localhost.
try to change to a domain and check it.
Thanks for your support.
I am getting same error on live server also 🙁
Authentication required
Yesterday it was working on localhost itself.
Regards,
Anto S
Oh I think you already authenticated your APP!
Once the application created you have to authenticate by yourself by allow permission for uploading data to your Dropbox account.
Then only others can upload files through your applications.
behind the scene a token file generated and store your token on the file.
So first you have to authenticate the APP by uploading a sample file!.
Yes Now its work fine… Thanks for your support 🙂
Hi,
Hope you are doing good..
We tried to modify this codings to class files. We placed tokens, DropboxClient, OAuthSimple in vendors folder. created class.dropbox.php in class folder.Inside class.dropbox.php we included require_once(DOCROOT . ‘vendors/dropbox/DropboxClient.php’);. Now we can able to create token on each reloading. But after that getting error as below.
Fatal error: Uncaught exception ‘OAuthSimpleException’ in D:\wamp\www\anto\em\vendors\dropbox\OAuthSimple.php on line 399
can you please guess whats going wrong here>
There is no need of creating token on every request.
You have to create the token first time, Then authorize the application by a sample upload by your account.
So next time own-words others can use the app the token file exists in the proper path.
I think the flow get messed , before authenticate you tried for an upload or something ?
tks. tut is very good.
stdClass Object ( [error] => Forbidden )
Error after uploading the text file.
No error log is there
This issue is happen due to APP permission.
Please follow up the ticket,
https://forums.dropbox.com/topic.php?id=112868#post-597766
ho gya bhai …..???????
stdClass Object ( [error] => Forbidden )
Error after uploading the text file.
Did you authorize the application by first upload ? then only token file generate.
Also please check the error_log for detailed information.
Hi,
I’m getting the print_r out put as,
stdClass Object
(
[error] => Forbidden
)
Can you please help with this issue?
did you create the apps properly also authorized ?
Please check the error log and let me know the details.
Can you make a contact form with file upload to dropbox? With these fields: name*, email*, phone, message*, upload (to dropbox). * are required 🙂
Can you do that? 😉
Thanks a lot
Thanks. Just what i need to upload backups automatically 🙂 from any php hosting.
Thank you.
Please provide complete Working Client Files For PHP
download the zip file all the required files are there and just follow the article for the integration.
This code is working for other please check the error_log for more details of errors in your Dropbox api integration
Nice Script. Works very fine. Thanks.
Nice script. Works like a breeze. Thanks!
Authentication required with some url .
how to solve the problem .
Thanks in advance
Once you integrate the APP Properly on the site, you have to upload a sample image then a token file will create on the path you have given, It means you authorized the app.
If you have done this your token file store required authentications permanently so anyone can upload files to Dropbox using your Apps.
make sure you have done all these steps. After that any errors check the error_log and let me know the status.
Hello,
Error occurred when upload file :
“Class Object ( [error] => Unauthorized )”
Please give me suggestion.
did you authorized the app ? It was working long time and suddenly it show this message ,
then simply remove all your token file and Just recreate once , by authorizing the app yourself.
It will fix the issue.
Thank’s for replay jobin.I try this.