What is Laravel?
Laravel is a web application framework written using PHP that offers an easy to read foundation for building web apps.
Laravel can be configured to use S3-compatible storage such as Filebase. Read below to learn how to configure Laravel for use with Filebase.
This guide was configured and tested using Ubuntu 20.04. Commands may vary depending on your operating system.
1. Create a new Laravel project with the following command:
composer create-project laravel/laravel PROJECT-NAME
2. Use the cd command to move into the newly created project directory.
3. Create a new file in the ./resources/views
directory and name it fileUpload.blade.php
with the following contents:
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">File Upload</div>
<div class="card-body">
<form action="{{ route('upload') }}" method="post" enctype="multipart/form-data"
@csrf
<input type="file" name="file" class="form-control">
<input type="submit" class="btn btn-primary">
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
4. Move back into your project’s top directory and open the ./routes/web.php
file.
Replace the existing contents with the following:
<?php
Route::get('/', function () {
return view('fileUpload');
});
Route::post('upload',function(){
request()->file('file')->store(
'my-file',
'Filebase'
);
})->name('upload');
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
‘My-file’ refers to the directory that your files will be uploaded to. Change this value to reflect your intended configuration.
5. Move back into your project’s top directory and open the .env file.
Replace the existing contents with the following:
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:ZBWX+Pgx8ABy8CThdSi9ixRCyQmKQ1iWDsv1TSCMBUM=
APP_DEBUG=true
APP_URL=http://localhost
LOG_CHANNEL=stack
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1
MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
FB_ACCESS_KEY_ID=XXXXXXXXXXXXXXXXXXXX
FB_SECRET_ACCESS_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
FB_DEFAULT_REGION=us-east-1
FB_BUCKET=filebase-sample-bucket
FB_URL=s3.filebase.com
You will need to change the FB_ACCESS_KEY_ID
and FB_SECRET_ACCESS_KEY
values to reflect your Filebase Access and Secret Keys.
6. Move back into your project’s top directory and open the ./config/filesystems.php
file.
Replace the existing contents with the following:
<?php
return [
'default' => env('FILESYSTEM_DRIVER', 'local'),
'cloud' => env('FILESYSTEM_CLOUD', 's3'),
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
'Filebase' => [
'driver' => 's3',
'key' => env('FB_ACCESS_KEY_ID'),
'secret' => env('FB_SECRET_ACCESS_KEY'),
'region' => env('FB_DEFAULT_REGION'),
'bucket' => env('FB_BUCKET'),
'endpoint' => 'https://s3.filebase.com'
],
],
];
7. Next, install the Laravel driver with the commands:
php artisan make:auth
composer require league/flysystem-aws-s3-v3 "^1.0"
8. Then start your Laravel server with the following command:
php artisan serve
10. You will see a screen that prompts file uploads.
Each file uploaded will be uploaded to your Filebase bucket that you specified in the .env
file.