Installation Add the bundle via Composer (note: package is archived, verify compatibility with your Symfony/Laravel version):
composer require antonioturdo/file-bundle
Register the bundle in config/bundles.php (Symfony) or manually load it in Laravel via a service provider.
Configuration
Locate the default config at config/packages/antonioturdo_file.yaml (Symfony) or adapt the config/antonioturdo_file.php structure in Laravel. Key settings:
'upload_dir' => storage_path('app/uploads'),
'allowed_extensions' => ['jpg', 'png', 'pdf'],
'max_file_size' => '2M',
First Use Case: File Upload
Inject the FileManager service into a controller/service:
use AntonioTurdo\FileBundle\Service\FileManager;
public function uploadFile(Request $request, FileManager $fileManager) {
$file = $request->file('document');
$path = $fileManager->upload($file, 'documents');
return response()->json(['path' => $path]);
}
File Uploads
fileManager->upload($file, $directory) for direct storage.fileManager->setRules($rules) (e.g., MIME types, dimensions).fileManager->upload($file, $directory, $customName).File Management
fileManager->delete($path) to remove files.fileManager->listFiles($directory) for directory scans (returned as array of paths).Integration with Laravel
AppServiceProvider:
$this->app->bind('fileManager', function ($app) {
return new \AntonioTurdo\FileBundle\Service\FileManager(
$app['config']['antonioturdo_file.upload_dir'],
$app['config']['antonioturdo_file.allowed_extensions']
);
});
FormRequest classes:
public function rules() {
return [
'file' => 'required|file|max:2048|mimes:jpg,png,pdf',
];
}
Storage Adaptors
storage_path()).FileManager to support S3, FTP, etc., by overriding the save() method.Archived Package
Configuration Overrides
config/antonioturdo_file.php may not auto-generate. Manually define:
return [
'upload_dir' => storage_path('app/uploads'),
'allowed_extensions' => explode(',', env('ALLOWED_FILE_TYPES', 'jpg,png,pdf')),
];
File Validation Gaps
mimes: rule or add a custom validator:
$fileManager->setRules([
'mime_types' => ['image/jpeg', 'application/pdf'],
]);
Directory Permissions
storage_path('app/uploads') is writable:
mkdir -p storage/app/uploads && chmod -R 775 storage/app/uploads
\Log::debug('File uploaded to:', [$path]);
if (!file_exists($path)) {
throw new \RuntimeException("File not saved at {$path}");
}
Custom File Naming
Override the generateFilename() method in a subclass of FileManager:
protected function generateFilename($file, $directory, $customName = null) {
return parent::generateFilename($file, $directory, $customName ?? uniqid());
}
Post-Upload Actions
Hook into the afterUpload event by extending the service:
$fileManager->upload($file, 'docs')->then(function ($path) {
// Trigger additional logic (e.g., thumbnail generation)
});
Laravel Events Dispatch custom events post-upload:
event(new FileUploaded($path, $request->user()));
How can I help you explore Laravel packages today?