Skip to content

Getting started

FileMagic is a file-management package built exclusively for Laravel.

Requirements

  • PHP 8.3 or later
  • Laravel 12 or 13
  • PHP ext-fileinfo
  • A configured Laravel Filesystem disk

Composer checks ext-fileinfo during installation because FileMagic detects MIME types from actual file contents instead of trusting filenames or client-provided MIME types.

Remote HTTP(S) imports through fromUrl() additionally need PHP ext-curl. Without it, all other features remain available, while storing a remote source throws RemoteDownloadUnavailable. Verify the extension used by your CLI with php --ri curl.

Image resizing additionally needs intervention/image 4.0 or later and PHP GD or Imagick. ZIP downloads additionally need PHP ext-zip.

Installation

bash
composer require mattmy/laravel-file-magic
php artisan vendor:publish --tag=file-magic-config
php artisan vendor:publish --tag=file-magic-migrations
php artisan migrate

Laravel auto-discovers Mattmy\FileMagic\FileMagicServiceProvider and the FileMagic facade. If discovery is disabled, register the provider manually in bootstrap/providers.php:

php
use Mattmy\FileMagic\FileMagicServiceProvider;

return [
    FileMagicServiceProvider::class,
];

Configuration

The published config/file-magic.php contains:

php
<?php

declare(strict_types=1);

return [
    'disk' => \env('FILE_MAGIC_DISK', \env('FILESYSTEM_DISK', 'local')),
    'directory' => \env('FILE_MAGIC_DIRECTORY', 'files'),
    'visibility' => \env('FILE_MAGIC_VISIBILITY', 'private'),
    'max_size' => 100 * 1024 * 1024,
    'allowed_mime_types' => [],
    'blocked_mime_types' => [
        'application/x-httpd-php',
        'application/x-php',
    ],
    'collision' => 'unique',
    'checksum_algorithm' => 'sha256',
    'temporary_url_ttl' => 5,
    'model' => Mattmy\FileMagic\Models\StoredFile::class,
    'table' => 'stored_files',
    'image' => [
        'quality' => 80,
        'max_width' => 1920,
    ],
    'zip' => [
        'max_files' => 100,
        'max_size' => 1024 * 1024 * 1024,
    ],
    'remote' => [
        'connect_timeout' => 5,
        'timeout' => 30,
        'max_redirects' => 3,
        'allowed_hosts' => [],
        'allowed_ports' => [80, 443],
    ],
];
OptionPurpose
diskDefault Filesystem disk
directoryDefault relative storage directory
visibilityprivate or public
max_sizeMaximum detected size in bytes
allowed_mime_typesMIME allowlist; empty allows every non-blocked type
blocked_mime_typesMIME types rejected in every default operation
collisionunique, error, or overwrite
checksum_algorithmPHP hash algorithm; invalid values fall back to sha256
temporary_url_ttlDefault temporary URL lifetime in minutes
modelClass extending StoredFile
tableFile-record table
image.qualityDefault image output quality
image.max_widthDefault maximum image width
zip.max_filesMaximum files in one ZIP download
zip.max_sizeMaximum uncompressed source bytes in one ZIP download
remote.connect_timeoutDefault connection timeout in seconds
remote.timeoutDefault total download timeout in seconds
remote.max_redirectsDefault redirect limit from 0 to 10
remote.allowed_hostsExact public-host allowlist; empty permits public hosts that pass SSRF validation
remote.allowed_portsNon-empty port allowlist; defaults to standard HTTP and HTTPS ports

Environment overrides:

dotenv
FILE_MAGIC_DISK=s3
FILE_MAGIC_DIRECTORY=uploads
FILE_MAGIC_VISIBILITY=private

Core workflow

FileMagic operations follow three stages:

  1. Create a PendingFile with fromUpload(), fromPath(), fromUrl(), fromContent(), fromBase64(), text(), json(), or csv().
  2. Configure storage with methods such as onDisk(), inDirectory(), named(), and visibility().
  3. Call store() to persist the physical file and its database record.
php
$file = FileMagic::fromUpload($uploadedFile)
    ->onDisk('local')
    ->inDirectory('documents')
    ->named('contract')
    ->store();

Only the source method and store() are required. Every configuration method in between is optional.

GoalMethods
Create a pending filefromUpload(), fromPath(), fromUrl(), fromContent(), fromBase64()
Generate a documenttext(), json(), csv()
Choose storageonDisk(), inDirectory()
Choose a filenamenamed()
Persist the filestore()
Find and operate on stored filesfind()
Download multiple files as ZIPfind()->downloadZip()

Last updated:

Released under the MIT License.