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.