PHP Classes

Dropbox upload

Recommend this page to a friend!

      PHP OAuth Library  >  PHP OAuth Library package blog  >  How to Implement PHP ...  >  All threads  >  Dropbox upload  >  (Un) Subscribe thread alerts  
Subject:Dropbox upload
Summary:Trying to figure out how to upload a file to Dropbox
Messages:9
Author:Kim Aldis
Date:2014-11-05 09:03:27
 

  1. Dropbox upload   Reply   Report abuse  
Picture of Kim Aldis Kim Aldis - 2014-11-05 09:03:27
I'm trying to work out how to upload a file to dropbox. I've managed to get as far as authorising but I can't for the life of me figure out what the upload process is.

If I'm missing something obvious my apologies for being a bit of an oauth newb.

thanks.

  2. Re: Dropbox upload   Reply   Report abuse  
Picture of Kim Aldis Kim Aldis - 2014-11-05 10:59:23 - In reply to message 1 from Kim Aldis
I should add, I'm using this code:

$success = $client->CallAPI(
'https://api-content.dropbox.com/1/files_put/auto', 'POST',
array(
'FileName' => $PathToImage
),
array(
'FailOnAccessError'=>true;
),
$user
);




And this is the error:


it was not possible to access the API call: it was returned an unexpected response status 400 Response: {"error": "Upload failed. Content-Type application/x-www-form-urlencoded must not be empty and not one of ('application/x-www-form-urlencoded', 'multipart/form-data')"}

  3. Re: Dropbox upload   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2014-11-05 21:10:46 - In reply to message 2 from Kim Aldis
For file uploads you need to tell which of the parameters are files using the "Files" parameter. Take a look at the documentation for the details and let me know if you still have doubts.

  4. Re: Dropbox upload   Reply   Report abuse  
Picture of Kim Aldis Kim Aldis - 2014-11-06 21:20:06 - In reply to message 3 from Manuel Lemos
Thanks. Now I have:

$success = $client->CallAPI(
'https://api-content.dropbox.com/1/files_put/auto', 'PUT',
array(
'source' => $pathToImage
),
array(
'FailOnAccessError'=>true,
'Files' => array( 'source'=>array() ),
),
$user
);

and the error I see is:

it was not possible to access the API call: it was returned an unexpected response status 400 Response: {"error": "Upload failed. Content-Type multipart/form-data; boundary=--500cc8709cfe156e5e38fdf9a1adbf48 must not be empty and not one of ('application/x-www-form-urlencoded', 'multipart/form-data')"}

  5. Re: Dropbox upload   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2014-11-07 03:37:37 - In reply to message 4 from Kim Aldis
Oh, sorry, I just realized that you are not using the POST method.

For the PUT method you need to use RequestBody to pass the file data and RequestBodyContentType if you need to specify a content-type.

  6. Re: Dropbox upload   Reply   Report abuse  
Picture of Kim Aldis Kim Aldis - 2014-11-07 07:14:52 - In reply to message 5 from Manuel Lemos
That did it. Many thanks.

  7. Re: Dropbox upload   Reply   Report abuse  
Picture of Kim Aldis Kim Aldis - 2014-11-07 07:45:17 - In reply to message 6 from Kim Aldis
FYI:

If I use RequestContentType I get an error "the request body is defined automatically from the parameters" and the upload fails.

If I don't use RequestContentType the upload works but I get a warning: "Notice: Undefined index: RequestContentType in /home/sites/kim-aldis.co.uk/public_html/wp5/wp-content/plugins/peanut/pnSocial/oauth-api/oauth_client.php on line 1441"

Not really a big deal, it's working so I'm good.

  8. Re: Dropbox upload   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2014-11-07 08:39:46 - In reply to message 7 from Kim Aldis
Are you setting it to application/x-www-form-urlencoded or multipart/form-data ? Those are only for POST form submissions.

For regular files you should use values like application/octet-stream, text/html, etc..

  9. Re: Dropbox upload   Reply   Report abuse  
Picture of Kim Aldis Kim Aldis - 2014-11-07 09:58:27 - In reply to message 8 from Manuel Lemos
That did it. Again, thanks.


In case anyone finds it useful; $fileName is the file name to upload to on Dropbox, $filePath is the full path to the local file.


$success = $client->CallAPI(
‘https://api-content.dropbox.com/1/files_put/auto/’ . $fileName,
'PUT',
array(),
array(
'FailOnAccessError'=>true,
'RequestBody' => file_get_contents( $filePath ),
'RequestContentType' => 'application/octet-stream'
),
$user
);