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 packageIts 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-dropboxStep2: 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.
- 'dropbox' => [
- 'driver' => 'dropbox',
- 'key' => 'your-key',
- 'secret' => 'your-secret'
- ],
Put following line of code in your routes.php
- 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.
- public function dropboxFileUpload()
- {
- $Client = new Client(config('filesystems.dropbox.key'), config('filesystems.dropbox.secret'));
- $file = fopen(public_path('images/angular_crud.png'), 'rb');
- $size = filesize(public_path('images/angular_crud.png'));
- $dropboxFileName = '/dropboxfile-name.png';
- $Client->uploadFile($dropboxFileName,WriteMode::add(),$file, $size);
- }
Don't forget to use traits to include package at top of controller.
- use Dropbox\Client;
- use Dropbox\WriteMode;
If you want to store the file link in your database then use these function to view share link.
- $Client->createShareableLink($dropboxFileName);
- $Client->createTemporaryDirectLink($dropboxFileName);
Now you can use this example to upload file in dropbox in Laravel 5.2