Looking to hire Laravel developers? Try LaraJobs

laravel-filesanitizer maintained by portavice

Description
Laravel integration for sytxlabs/filesanitizer
Author
Last update
2026/04/23 10:49 (dev-main)
License
Links
Downloads
131

Comments
comments powered by Disqus

Laravel FileSanitizer

MIT Licensed Check code style Tests Latest Version on Packagist Total Downloads

Laravel integration for portavice/filesanitizer.

The upstream package is installed as portavice/filesanitizer and uses the class Portavice\FileSanitizer\FileSanitizer.

Installation

composer require portavice/laravel-filesanitizer
php artisan vendor:publish --tag=filesanitizer-config

Usage

use Portavice\LaravelFileSanitizer\Facades\FileSanitizer;

$result = FileSanitizer::process(storage_path('app/uploads/file.pdf'), null, true);

if (! FileSanitizer::safe($result)) {
    foreach (FileSanitizer::issues($result) as $issue) {
        echo is_object($issue) ? $issue->code : ($issue['code'] ?? 'unsafe');
        echo PHP_EOL;
    }
}

Validation rule

use Portavice\LaravelFileSanitizer\Rules\SafeFile;

$request->validate([
    'upload' => ['required', 'file', new SafeFile()],
]);

Or as a validator string rule:

$request->validate([
    'upload' => ['required', 'file', 'safe_file'],
]);

UploadedFile macro

$result = $request->file('upload')->sanitize();

Config

return [
    'sanitize_always' => env('FILESANITIZER_SANITIZE_ALWAYS', false),
];

This config will always sanitize files when set to true, even if the file is already considered safe. This can be useful if you want to ensure that all files are sanitized, regardless of their initial safety status.