Installation:
composer require bengor-file/file-bundle
Register the bundle in config/bundles.php:
return [
// ...
BenGorFile\FileBundle\BenGorFileBundle::class => ['all' => true],
];
Configuration: Publish the default config:
php bin/console ben-gor-file:install
Or manually configure in config/packages/ben_gor_file.yaml:
ben_gor_file:
storage: '%kernel.project_dir%/var/files'
allowed_extensions: ['jpg', 'png', 'pdf']
max_file_size: 10M
First Use Case: Upload a file via a controller:
use BenGorFile\FileBundle\Service\FileUploader;
public function upload(Request $request, FileUploader $uploader)
{
$file = $request->file('file');
$path = $uploader->upload($file, 'uploads');
return new JsonResponse(['path' => $path]);
}
File Uploads:
FileUploader service for handling uploads with validation:
$uploader->upload($file, 'custom/directory', ['allowed_extensions' => ['jpg']]);
ChunkedFileUploader.File Management:
$this->fileManager->delete($filePath);
$this->fileManager->rename($oldPath, $newPath);
$metadata = $this->fileMetadata->getMetadata($filePath);
Integration with Forms:
FileType form field for Symfony forms:
use BenGorFile\FileBundle\Form\Type\FileType;
$builder->add('document', FileType::class, [
'label' => 'Upload Document',
'allowed_extensions' => ['pdf', 'docx'],
]);
Storage Adapters:
ben_gor_file:
storage_adapter: 'aws_s3'
aws_s3:
bucket: 'my-bucket'
region: 'us-east-1'
Event Listeners:
public function onFileUpload(FileUploadEvent $event)
{
if ($event->isValid()) {
// Process file
}
}
services.yaml:
services:
App\EventListener\FileUploadListener:
tags:
- { name: 'kernel.event_listener', event: 'file.upload', method: 'onFileUpload' }
Configuration Overrides:
ben_gor_file.yaml is merged correctly. Use !imports or parameters to override defaults without losing other settings.File Validation:
config/packages/ben_gor_file.yaml:
ben_gor_file:
validation:
allowed_extensions: ['jpg', 'png', 'gif']
max_file_size: 5M
forbidden_patterns: ['/malicious\.php$/']
Permissions:
storage directory is writable:
chmod -R 775 %kernel.project_dir%/var/files
Chunked Uploads:
ChunkedFileUploader but ensure:
session.save_path).Symfony 5+ Compatibility:
FileUploader service if autowiring fails:
services:
BenGorFile\FileBundle\Service\FileUploader:
arguments:
$container: '@service_container'
Log Upload Events:
Enable debug mode and check logs for file.upload events:
monolog:
handlers:
main:
level: debug
Validate File Paths: Use absolute paths for storage. Relative paths may break in production.
Check Storage Adapter:
If files disappear, verify the storage_adapter is correctly configured and the underlying service (e.g., AWS SDK) is initialized.
Custom Storage Adapters:
Implement BenGorFile\FileBundle\Storage\StorageInterface:
class CustomStorage implements StorageInterface {
public function save($file, $path) { /* ... */ }
public function delete($path) { /* ... */ }
}
Register in services.yaml:
ben_gor_file.storage_adapter: '@app.custom_storage'
Custom Validators:
Extend BenGorFile\FileBundle\Validator\Constraints\File:
class CustomFileValidator extends FileValidator {
protected function validateCustomRule($value, Constraint $constraint) {
// Custom logic
}
}
Twig Extensions: Add file-related Twig functions:
// src/Twig/FileExtension.php
class FileExtension extends \Twig\Extension\AbstractExtension {
public function getFunctions() {
return [
new \Twig\TwigFunction('file_url', [$this, 'getFileUrl']),
];
}
}
Console Commands: Create custom commands for bulk operations:
use BenGorFile\FileBundle\Service\FileManager;
class CleanupCommand extends Command {
protected function execute(InputInterface $input, OutputInterface $output, FileManager $manager) {
$manager->deleteOldFiles('logs', '-1 month');
}
}
Cache Metadata:
Cache file metadata (e.g., size, mime type) to avoid repeated calls to FileMetadata:
$this->cache->remember("file_metadata_{$path}", 3600, function() use ($path) {
return $this->fileMetadata->getMetadata($path);
});
Async Processing: Use Symfony Messenger to process uploads asynchronously:
$message = new ProcessFileMessage($filePath);
$this->messageBus->dispatch($message);
Batch Operations:
For bulk uploads, use FileUploader::uploadMultiple() and process in chunks to avoid memory issues.
```markdown
## Maintenance Notes
- **Deprecation**: The package is outdated (last release 2018). Fork and update for Symfony 5/6 if critical.
- **Testing**: Run PHPSpec tests locally to understand expected behavior:
```bash
composer require phpspec/phpspec --dev
vendor/bin/phpspec run
How can I help you explore Laravel packages today?