Dropbox api File Upload using league/flysystem-dropbox in Laravel

Share Me
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Today now In this post i am going to give you a example of how to upload the file in our dropbox account by using the league/flysystem-dropbox package. As well as we will also get the uploaded file share link and view link on that way if we need to store it on database then we can store on db. So now this posts i will give you a very simple example to store the file in dropbox account by using Dropbox Client API. Just You need to follow the few step and we can run example.

We are know that Dropbox is a file hosting service and it is operated by American company Dropbox. They provides us cloud storage, file synchronization and the personal cloud software. Now a days Dropbox is very popular for the file synchronization and cloud storage. We can easily store the large file on dropbox and share with the someone, as well as we can store this database backup in dropbox. Most of the people choose the dropbox for this cloud storage.

Step 1: Installation Package

Now in this step we need to install the league/flysystem-dropbox package and we know on that way we can use the Dropbox Client API method.

So first need to run the bellow command.

composer require league/flysystem-dropbox
Step 2: Token and Secret Configration

Okay now need to create account on dropbox website on that way we can get the token and also secret key. So if we don’t have any account then we can create from here : Create App.

So after the create new account we can find the token and secret key. we have to copy all that and need to add on .env file this way:

.env

DROPBOX_TOKEN=app_tokenDROPBOX_SECRET=app_secret
Step 3: Create route and controller

Now in this step we need to create the route and also controller for the simple example on that way we can understand this very well.

So need to create route on this way.

app/Http/routes.php

Route::get('dropboxFileUpload', '[email protected]');

Okay, now our HomeController we need to add the dropboxFileUpload() method on this way, in this example i have an img folder on the public folder and admin.png image inside on that folder, so after the run this example it will go on in dropbox account:

app/Http/Controllers/HomeController.php

namespace App\Http\Controllers;


use App\Http\Requests;
use Illuminate\Http\Request;
use Dropbox\Client;
use Dropbox\WriteMode;


class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }


    public function dropboxFileUpload()
    {
        $Client = new Client(env('DROPBOX_TOKEN'), env('DROPBOX_SECRET'));


        $file = fopen(public_path('img/admin.png'), 'rb');
        $size = filesize(public_path('img/admin.png'));
        $dropboxFileName = '/myphoto4.png';


        $Client->uploadFile($dropboxFileName,WriteMode::add(),$file, $size);
        $links['share'] = $Client->createShareableLink($dropboxFileName);
        $links['view'] = $Client->createTemporaryDirectLink($dropboxFileName);


        print_r($links);
    }
}

Read Also: Generate Captcha code and Validation by using BotDetect package in laravel

Thanks for read. I hope it help you. For more you can follow us on facebook

About code chef

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by code chef →