Laravel 5.2 working with Dropbox API to upload file using league/flysystem-dropbox package with example

Laravel 5.2 working with Dropbox API to upload file using league/flysystem-dropbox package with example

Working with Dropbox API to upload file using league/flysystem-dropbox package with example in Laravel 5.2

Here is a sample example to upload file over dropbox, just follow few step to backup your file data in Laravel 5.2 using league/flysystem-dropbox package.

Benefits with Dropbox which is know as cloud storage is freely upload your files into cloud storage instead of server that means you can backup your application or backup your database to dropbox in Laravel 5.2.

Dropbox have many awesome features to working with files as you can easily share your files with friends or upload bulk of files which take less time to uploading, rename your files, edit your files and many more features there.

Flysystem is a filesystem that give you permission to swap out a local filesystem for a remote one.

There are 3 simple steps to done with file upload in dropbox. Follow each step to store or upload file in your dropbox account using Dropbox Client API.

Step1: Install league/flysystem-dropbox package

Its very easy to install league/flysystem-dropbox package with composer, you have to simply fire this command to use Dropbox Client API.

composer require league/flysystem-dropbox
Step2: Create a Dropbox API App

You will need to have app_token and app_secret to authenticate your account with Dropbox. So create a Dropbox API App first to get app_token and app_secret.

Click here to create Dropbox API App

Now you will get your token and secret key after creating Dropbox API App, just copy that token and put in your filesystems.php within config directory.

  1. 'dropbox' => [
  2. 'driver' => 'dropbox',
  3. 'key' => 'your-key',
  4. 'secret' => 'your-secret'
  5. ],
Step3: Route and Controller

Put following line of code in your routes.php

  1. Route::get('dropbox-upload-file', 'FileController@dropboxFileUpload');

Now create a method in your controller to upload file in dropbox. Here I am creating a FileController and within this controller i add dropboxFileUpload method.

Put these code in your controller.

  1. public function dropboxFileUpload()
  2. {
  3. $Client = new Client(config('filesystems.dropbox.key'), config('filesystems.dropbox.secret'));
  4. $file = fopen(public_path('images/angular_crud.png'), 'rb');
  5. $size = filesize(public_path('images/angular_crud.png'));
  6. $dropboxFileName = '/dropboxfile-name.png';
  7. $Client->uploadFile($dropboxFileName,WriteMode::add(),$file, $size);
  8. }

Don't forget to use traits to include package at top of controller.

  1. use Dropbox\Client;
  2. use Dropbox\WriteMode;

If you want to store the file link in your database then use these function to view share link.

  1. $Client->createShareableLink($dropboxFileName);
  2. $Client->createTemporaryDirectLink($dropboxFileName);

Now you can use this example to upload file in dropbox in Laravel 5.2

Phone: (+91) 8800417876
Noida, 201301