0

What I have till now

Right now I have a working oauth2 authentication between a laravel user and the dropbox API. Every user can upload files to their personal folder.

The Problem

After Uploading a file with laravel with the Dropbox API v2 I can see that there is a empty (0 Bytes) file uploaded.

Used to accomplish this task:

  • Laravel
  • Guzzle
  • Dropbox API Library

What am I missing?

The Code

My function for processing a form looks like this:

$formFile = $request->file('fileToUpload');
$path = $formFile->getClientOriginalName();
$file = $formFile->getPathName();
$result = Dropbox::files()->upload($path, $file);
return redirect('dropboxfiles');

And my files->upload function in my Dropbox Library looks like this:

$client = new Client;

   $response = $client->post("https://content.dropboxapi.com/2/files/upload", [
      'headers' => [
         'Authorization' => 'Bearer '.$this->getAccessToken(),
         'Content-Type' => 'application/octet-stream',
         'Dropbox-API-Arg' => json_encode([
            'path' => $path,
            'mode' => 'add',
            'autorename' => true,
            'mute' => true,
            'strict_conflict' => false
          ])
       ],
       'data-binary' => '@'.$file
   ]);

The file, as I said, gets uploaded successfully. Correct name, but 0 Bytes. So empty file.

Thank you so much in advance for your help!

Update

With the following code I made it work. My question is though if there is a better "Laravel-Like" Solution instead of using fopen?

$response = $client->post("https://content.dropboxapi.com/2/files/upload", [
            'headers' => [
                'Authorization' => 'Bearer '.$this->getAccessToken(),
                'Dropbox-API-Arg' => json_encode([
                    'path' => $path,
                    'mode' => 'add',
                    'autorename' => true,
                    'mute' => true,
                    'strict_conflict' => false
                ]),
                'Content-Type' => 'application/octet-stream',
            ],
            'body' => fopen($file, "r"),
        ]);
4
  • I think you are looking for github.com/spatie/flysystem-dropbox which uses League/filesystem under the hood Commented Jan 25, 2020 at 0:50
  • @alithedeveloper I'm not quite sure. Right now I'm using daveismyname's Dropbox Package. With this package I allow a User to connect to their own Dropbox account and save app files there. With spaties flysystem i think i will just have the possibility to have an additional storage filesystem if i'm not wrong.
    – ismaelw
    Commented Jan 25, 2020 at 13:07
  • [Cross-linking for reference: dropboxforum.com/t5/API-Support-Feedback/… ]
    – Greg
    Commented Jan 27, 2020 at 16:26
  • Thank you so much @Greg for the explanation! Feel free to add your solution from the cross-link here so I can mark it as an answer!
    – ismaelw
    Commented Jan 27, 2020 at 20:48

1 Answer 1

0

How @Greg mentioned (see cross-linking reference) I was able to solve this issue by using

'body' => fopen($file, "r"),

instead of

'data-binary' => '@'.$file

This is, how Greg mentioned, because data-binary is used in Curl requests. Other HTTP Clients, like Guzzle in my case use different names.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.