Dropbox is an American firm that provides file hosting services. They offer personal cloud software, file syncing, and cloud storage. Today, Dropbox is a widely popular option for cloud storage and file synchronization. Large files can be stored and shared on Dropbox, and database backups can also be kept there. Most individuals choose for Dropbox while using cloud storage.

I'll demonstrate how to upload a file to our Dropbox account using the league/flysystem-dropbox package in this post. Additionally, we receive the share and view links for uploaded files, allowing us to keep them in databases if necessary. In this post, I'll show you a straightforward example of utilizing the Dropbox Client API to store a file in your account. You only need to follow a few simple steps to run an example.

Installation Package

In order to use the Dropbox Client API method, we must first install the league/flysystem-dropbox package. To do this, perform the following command.

composer require league/flysystem-dropbox

Token and Secret Configration

To obtain the token and secret, we must first build an app on the Dropbox website; if you don't already have an account, you may do so here: Create App.

You can find a secret and a token after creating a new app. You must duplicate it and then continue. env file this way:

.env

DROPBOX_TOKEN=app_token
DROPBOX_SECRET=app_secret

Create route and controller

For a straightforward example so that you can fully grasp, we must establish a route and controller in this stage. Create a route in this manner initially.

app/Http/routes.php

Route::get('dropboxFileUpload', 'HomeController@dropboxFileUpload');

Now that your HomeController has been added, let's look at an example. In this example, I have an img folder on the public folder with an admin.png image inside of it. When I run this example, my Dropbox account will receive the file:

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);
    }
}

Run the given example and see the results.


Recommended Posts

View All

Laravel Eloquent Query - Laravel Group By with Order By Desc


You've come to the right place if you're looking for an example of a laravel eloquent group by order by desc.

How To Use WYSIHTML5 Editor In Laravel 9?


wysihtml5 editor example, laravel wysihtml5 editor example, wysihtml5 laravel, wysihtml5 editor example in laravel, wysihtml5 with laravel, wysihtml5...

Laravel Where Clause with MySQL Function Example


laravel eloquent where year,laravel eloquent mysql functions,laravel eloquent where clause mysql,mysql function mysql laravel

Effortlessly Handle File Uploads in Laravel with FilePond


We'll walk through the process of setting up FilePond, creating a file upload form, and handling file uploads in Laravel

Unlocking the Power of Data Visualizing Laravel App Data with Chartello


Discover the potential of data visualization with Chartello, a Laravel app that helps you unlock insights from your data. Try it now and see the resul...