Upload seamless Storage and Database files in Laravel
It supports:
public, local, s3 diskscomposer require sextanet/laravel-files
Publish and run the migrations with:
php artisan vendor:publish --tag="files-migrations"
php artisan migrate
use SextaNet\LaravelFiles\HasFiles; // 👈 1. Import
class YourModel extends Model
{
use HasFiles; // 👈 2. Use it
}
We're ready! You can reuse it in each model, any times!
$uploaded_file = request()->your_file; // let's supose you have "a_video.mp4"
$file = $user->addFile($uploaded_file); // using your default disk from config/filesystems.php
Passing a destination and name
$file = $user->addFile($uploaded_file, 'destination', 'my_file'); // will store on destination/my_file.mp4
You can get the path, url and temporary_url for each file
$path = $file->path();
$url = $file->url();
$temporary_url = $file->temporaryUrl();
return $file->owner;
return $user->files;
return $user->latestFile;
return $file->download();
$name = 'custom_name';
$headers = [];
return $file->download($name, $headers);
By default it preserves the extension. You can disable that by passing preserveExtension to false
return $file->download(preserveExtension: false);
By default, it doesn't preserves original empty custom names.
[!CAUTION] If you enable that, you could overwrite your existent files.
LaravelFiles::preserveOriginalNames(true);
Type doesn't represent a real type or mimetype, it's only an optional field
$uploaded_file = request()->your_file;
// Store
$user->addFile($uploaded_file, type: 'document');
// Get
$user->files()->type('document')->get();
$temporary_url = $file->getTemporaryUrl(20); // 20 minutes
// Don't forget to import the LaravelFiles facade
use SextaNet\LaravelFiles\Facades\LaravelFiles;
// Set temporary URL for 120 minutes
LaravelFiles::setTemporaryUrlMinutes(120);
$temporary_url = $file->getTemporaryUrl(); // Will return 120 minutes
// Don't forget to import the LaravelFiles facade
use SextaNet\LaravelFiles\Facades\LaravelFiles;
// Store on another disk, like s3
LaravelFiles::setDisk('s3');
By default, we use the same CURRENT_DISK that you have in your .env file. If you want to force to use different values, you can add these keys with different values:
FILES_DISK=s3
FILES_TEMPORARY_URL_MINUTES=5
composer test
Please see CHANGELOG for more information on what has changed recently.
The MIT License (MIT). Please see License File for more information.
How can I help you explore Laravel packages today?