Installation
composer require barryvdh/laravel-elfinder
Publish the config and migrations:
php artisan vendor:publish --provider="Barryvdh\Elfinder\ElfinderServiceProvider" --tag="config"
php artisan vendor:publish --provider="Barryvdh\Elfinder\ElfinderServiceProvider" --tag="migrations"
Run migrations:
php artisan migrate
Basic Configuration
Edit config/elfinder.php to define storage disks (e.g., public, local):
'disks' => [
'public' => [
'driver' => 'public',
'root' => '/',
],
],
First Use Case: File Manager in a Blade View Add the ElFinder JS/CSS and initialize it in a Blade template:
@elfinder('elfinder', [
'disabled' => false,
'lang' => 'en',
'theme' => 'default',
'resizable' => true,
'uploadAllow' => ['image/*', 'application/pdf'],
])
Include the JS/CSS in your layout:
@vite(['resources/css/elfinder.min.css', 'resources/js/elfinder.min.js'])
Restricting File Access Use middleware or disk configuration to limit access to specific folders:
'disks' => [
'restricted' => [
'driver' => 'local',
'root' => storage_path('app/restricted'),
'visibility' => 'private', // Only accessible via ElFinder
],
],
Customizing File Uploads
Extend the upload handler via events (e.g., elfinder.uploading):
Elfinder::uploading(function ($file) {
// Modify file metadata or validate before upload
if ($file->mime === 'image/jpeg') {
$file->name = 'custom_' . $file->name;
}
});
Integration with Forms
Use ElFinder’s input command to generate hidden fields for file paths:
<input type="hidden" name="file_path" id="file_path">
Initialize ElFinder with a callback to populate the field:
$('#elfinder').elfinder({
url: '/elfinder',
commandsOptions: {
getfile: {
oncomplete: 'fileCallback'
}
}
}).on('fileCallback', function(event, obj) {
$('#file_path').val(obj.target.url);
});
Dynamic Disk Switching Pass the disk dynamically via JavaScript:
$('#elfinder').elfinder({
url: '/elfinder?disk=public',
});
Permission Issues
chmod -R 755 storage/).public disk, verify storage/app/public is symlinked to public/storage.CSRF Token Mismatch ElFinder requires CSRF protection. If using API routes, exclude them from verification:
// app/Http/Middleware/VerifyCsrfToken.php
protected $except = [
'elfinder/*',
];
CORS Errors (AJAX)
If ElFinder is used in a SPA, configure CORS for the /elfinder endpoint:
// app/Http/Middleware/Cors.php
$allowedMethods = ['*'];
$allowedOrigins = ['http://your-app.dev'];
Disk Driver Quirks
root paths are absolute (e.g., storage_path('app')).config/filesystems.php and reference them in elfinder.php:
'disks' => [
's3' => [
'driver' => 's3',
'bucket' => 'your-bucket',
],
],
debug: true in config/elfinder.php to log requests/responses./elfinder for errors (e.g., 403 Forbidden).php artisan cache:clear
php artisan config:clear
Custom Templates Override ElFinder’s UI templates by publishing and modifying:
php artisan vendor:publish --tag="elfinder-views"
Event Hooks
Listen for ElFinder events (e.g., elfinder.deleting, elfinder.uploaded) in EventServiceProvider:
protected $listen = [
'Barryvdh\Elfinder\Events\FileDeleted' => [
'App\Listeners\LogDeletedFile',
],
];
Middleware for Authorization Use Laravel middleware to restrict ElFinder access:
Elfinder::middleware(['auth', 'can:manage-files']);
Custom Drivers
Extend Barryvdh\Elfinder\Driver\AbstractDriver to support new storage backends (e.g., Dropbox).
How can I help you explore Laravel packages today?