Working with Dropbox in Laravel 5

Often we decide to store uploaded files into the cloud storage instead of our web server. There are many options available and Laravel comes with quite a few options but and can be extended with even more options, let's see how we can store uploaded files to popular could storage service Dropbox.

What we are building

We are going to build a simple app that will get our uploaded files and store it to Dropbox, to make things simple we will make this app in three simple steps.

1- First step for Upload form, where we will create a form to upload files. 

2- Tthe Second step is going to initiate our DropboxStorageRepository and will return  the success or failure. 

3- Last step will be to create our dedicated Dropbox repository that will house official Dropbox API related stuff such as creating a new connection to Dropbox and uploading files.

Step 1: Setting up our routes and creating our upload form

Here we have a upload form in fileUpload.blade.php view page. This form just containing a file input upload button for uploading any type of file.

app/Http/routes.php

Call the file method in StorageController.

<?php
Route::get('upload', 'StorageController@file');

app/Http/Controllers/StorageController.php

StorageController has file method for redirecting a request to our view fileUpload.blade.php page where our main form exist.

<?php
public function file()
{
    return view('fileUpload');
}

resources/views/fileUpload.blade.php

This is our upload form which have an obvious post method and 'files'=>true attribute which is similar to enctype= multipart/form-data required for upload file.

<html>
<head>
    <meta charset="UTF-8">
    <title>File Upload to dropbox</title>
</head>
<body>
@if(Session::has('success'))
    <h2>{!! Session::get('success') !!}</h2>
@endif
<div>Upload form</div>
{!! Form::open(array('url'=>'upload','method'=>'POST', 'files'=>true)) !!}
{!! Form::file('image', array('multiple'=>false)) !!}
{!! Form::submit('Submit', array('class'=>'send-btn')) !!}
{!! Form::close() !!}
</body>
</html>

Step 2: Getting uploaded files and initiating our Dropbox repository

This step introduces new method upload_to_dropbox function in StorageController which helping us to send our files to Dropbox.

app/Http/routes.php

<?php
Route::post('upload', 'StorageController@upload_to_dropbox');

app/Http/Controllers/StorageController.php

We are using dependency injection for connection to Dropbox to our application, we will create a DropboxStorageRepository instance named $connection. Through this $connection call the function getConnection() from DropboxStorageRepository class and containing return value into the $filesystem variable.

After that, We getting image file and file properties with the file() method and store it into the $file variable
Let’s focus on the Filesystem put method which required two parameters file name and it’s contents. This final process will send files to your Dropbox account.

<?php namespace App\Http\Controllers;

use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\File;
use App\Repositories\DropboxStorageRepository;
class StorageController extends Controller
{
public function file()
{
    return view('fileUpload');
}

public function upload_to_dropbox(DropboxStorageRepository $connection)
    {
        $filesystem = $connection->getConnection();
        $file = Input::file('image');
        $filesystem->put($file->getClientOriginalName(), File::get($file));

        // Calling to controller file method and redirect to form view page
        // After redirection user getting acknowledgment of success message 
        return Redirect::to('upload')->with('success', 'Upload successfully');
   	}

}

 

Step 3: Creating DropboxStorageRepository to manage Dropbox API

To perform Dropbox upload we need three packages,

We just need to pull filesystem-Dropbox package, Dropbox SDK and Filesystem will be pulled automatically as they are requirements for filesystem-dropbox. 

DropboxStorageRepository class has a construct method which have new client instance where we put our new client credential information like access token and Dropbox new app name and store it into client variable.

After the above process, We are ready create a new DropboxAdapter connection.

app/Repositories/DropboxStorageRepository.php

<?php namespace App\Repositories;

use League\Flysystem\Dropbox\DropboxAdapter;
use League\Flysystem\Filesystem;
use Dropbox\Client;

class DropboxStorageRepository{

    protected $client;
    protected $adapter;
    public function __construct()
    {
        $this->client = new Client('1extSuLc0-gAAAAAAAAB3eCEXyquWrZw69Tu3l5KovEqtr1_WT9wLBCrGcAga8U2', 'devartisans_testapp', null);
        $this->adapter = new DropboxAdapter($this->client);
    }
    public function getConnection()
    {
        return new Filesystem($this->adapter);
    }
}

That's it guys, now you can upload any file to Dropbox, visit Dropbox API documentation to get complete listing of actions you can perform.

If you get any error, please feel free to ask us in the comment section or create a new thread in the forum.

Something to say? Tell us in comment section.