Add Dropbox filesystem driver in your laravel 5 project

Web development

Laravel version 5, which is still under active development and set to be released in November, comes with lots of cool features. One of them is Flysystem integration. Flysystem is a filesystem abstraction PHP package which allows you to easily swap out a local filesystem for a remote one (Amazon S3, Rackspace, Dropbox and others). For now Laravel provides drivers (wrappers around flysystem code for even easier access) for local, s3 and rackspace filesystem but adding a new one was not a problem for me and it was easier than I first thought!

Here I will try to explain how I added an adapter for Dropbox.

First thing I did was going to to make a new app where I obtained app name and access token. Than I required dropbox sdk via composer and specified config options for dropbox driver – in config/filesystem.php under disks key I added the following:

'dropbox' => [
    'driver' => 'dropbox',
    'token' => 'YOUR_ACCESS_TOKEN',
    'app' => 'YOUR_APP_NAME'

Now go to the vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemManager.php file and add the following method where appopriate1.

use Dropbox\Client;
use League\Flysystem\Adapter\Dropbox as DropboxAdapter;
class FilesystemManager implements FactoryContract {
     * Create an instance of the given driver.
     * @param  array  $config
     * @return \Illuminate\Contracts\Filesystem\Cloud
    public function createDropboxDriver(array $config)
        $client = new Client($config['token'], $config['app']);

        return $this->decorate(new Flysystem(new DropboxAdapter($client)));

That’s it! Note that if you are doing an adapter for other filesystem, method name is very important. It must be create*Driver, where * represents actual driver’s name listed in disks key in config/filesystem.php. Also take a look at other create*Driver methods in that file and in Flysystem documentation for guidelines on how to write a new driver correctly.

Now you should access your files in Dropbox like this:

use Illuminate\Contracts\Filesystem\Factory as Filesystem;

get('/', function(Filesystem $filesystem)
    return $filesystem->disk('dropbox')->get('hello.txt');
  1. This is not done the right way though, but for scope of this tutorial it’s fine. A better way is to extend that class, add a new method and register new class with custom service provider. That way addition will not be lost if the core class, which we are extending, gets an update via composer. []
  • Is adding to the files in the vendor folder the only way to achieve this? I would prefer a way to do it with the added code in git.

    • Perhaps extending the filesystemmanager class and adding to it that way would work? Will give it a go.

      • Oops just read to the end of your article! Nevermind me lol

      • Jernej

        That was just some sort of proof of the concept. Because when you are extending classes and registering them with service provider, often you need to edit other related classes (which also need to be included). 😉

  • Hey Guys, I have found a better solution. Check it out in my gist.

  • Андрей Соколов

    Now go to the vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemManager.php file and add the following method where appopriate….

    This is bad practice.

    • Hey, thanks for stopping by and commenting.

      There is also a footnote on this statement you quoted at the bottom of the article which explains that this is not a right way to insert a code but by creating a service provider.