Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Files Laravel Package

sextanet/laravel-files

View on GitHub
Deep Wiki
Context7

Laravel Files

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Upload seamless Storage and Database files in Laravel

It supports:

Requirements

  • Laravel 11+
  • PHP 8.3+

Installation

composer require sextanet/laravel-files

Publish and run the migrations with:

php artisan vendor:publish --tag="files-migrations"
php artisan migrate

Usage

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!

Add (and Store)

$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

Get

You can get the path, url and temporary_url for each file

$path = $file->path();

$url = $file->url();

$temporary_url = $file->temporaryUrl();

Get owner

return $file->owner;

Get all files

return $user->files;

Get the latest file

return $user->latestFile;

Download

Without parameters

return $file->download();

With custom parameters

$name = 'custom_name';
$headers = [];

return $file->download($name, $headers);

Turn off the extensions

By default it preserves the extension. You can disable that by passing preserveExtension to false

return $file->download(preserveExtension: false);

Preserve original names

By default, it doesn't preserves original empty custom names.

[!CAUTION] If you enable that, you could overwrite your existent files.

LaravelFiles::preserveOriginalNames(true);

Advanced usage

Passing a type

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

Passing custom minutes in each implementation

$temporary_url = $file->getTemporaryUrl(20); // 20 minutes

Global usage

Passing custom minutes globally

// 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

Passing a different disk in a specific instance

// Don't forget to import the LaravelFiles facade
use SextaNet\LaravelFiles\Facades\LaravelFiles;

// Store on another disk, like s3
LaravelFiles::setDisk('s3');

Custom keys

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:

Another disk

FILES_DISK=s3

Custom minutes

FILES_TEMPORARY_URL_MINUTES=5

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

License

The MIT License (MIT). Please see License File for more information.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle