Installation:
composer require knplabs/knp-gaufrette-bundle gaufrette/fs gaufrette/adapters-{adapter}
Replace {adapter} with your storage backend (e.g., local, s3, ftp). For local filesystems, use gaufrette/adapters-local.
Configuration:
Add the bundle to config/bundles.php:
return [
// ...
Knp\Bundle\GaufretteBundle\KnpGaufretteBundle::class => ['all' => true],
];
Basic Usage:
Define a filesystem in config/packages/knp_gaufrette.yaml:
knp_gaufrette:
filesystems:
my_filesystem:
adapter: local
options:
directory: '%kernel.project_dir%/var/files'
First Use Case: Inject the filesystem into a service/controller:
use Knp\Bundle\GaufretteBundle\Filesystem\FilesystemService;
public function __construct(private FilesystemService $filesystemService) {}
public function uploadFile(Request $request) {
$filesystem = $this->filesystemService->getFilesystem('my_filesystem');
$file = $request->files->get('file');
$filesystem->writeNamedFile($file, 'unique_filename.ext');
}
File Operations:
writeNamedFile() or write() for direct content.read() or readAsString().delete() or deleteDir() for directories.file() or fileMetadata().Symfony Integration:
knp_gaufrette.filesystem.{name} service in templates.Knp\Bundle\GaufretteBundle\Form\Type\FileType for file uploads.knp_gaufrette.pre_write or knp_gaufrette.post_write for pre/post-processing.Adapter-Specific Patterns:
options (e.g., aws_access_key_id, aws_secret_access_key).host, username, password, and port in options.gaufrette/adapters-{adapter} for providers like Backblaze or DigitalOcean.Caching:
cache in filesystem config to cache metadata:
my_filesystem:
adapter: local
options:
directory: '%kernel.project_dir%/var/files'
cache: knp_gaufrette.cache.array # or 'knp_gaufrette.cache.apcu'
Streaming:
readStream() for large files to avoid memory issues:
$stream = $filesystem->readStream('large_file.ext');
return new StreamedResponse(function () use ($stream) {
fpassthru($stream);
});
Adapter Installation:
gaufrette/adapters-s3) causes runtime errors. Always install the adapter package explicitly.Permission Issues:
directory lacks write permissions. Verify permissions with:
chmod -R 775 %kernel.project_dir%/var/files
Circular References:
FilesystemService directly into controllers. Use dependency injection best practices (e.g., inject the specific filesystem service).File Overwrites:
write() overwrites files by default. Use writeNamedFile() to generate unique filenames and prevent collisions.Symlink Handling:
follow_links in options if needed:
options:
directory: '%kernel.project_dir%/var/files'
follow_links: true
Enable Debugging:
debug: true in knp_gaufrette.yaml to log filesystem operations:
knp_gaufrette:
debug: true
Check Adapter-Specific Errors:
try {
$filesystem->write(...);
} catch (\Gaufrette\FilesystemException $e) {
// Handle AWS/Azure-specific errors (e.g., 403 Forbidden)
}
Validate Configuration:
bin/console debug:container knp_gaufrette.filesystem to verify filesystems are registered.Custom Adapters:
Adapter interface to create custom storage backends (e.g., database storage).Event Subscribers:
knp_gaufrette.pre_write or knp_gaufrette.post_write to modify files before/after upload:
use Knp\Bundle\GaufretteBundle\Event\PreWriteEvent;
public function onPreWrite(PreWriteEvent $event) {
$file = $event->getFile();
// Resize images, validate content, etc.
}
Dynamic Filesystems:
FilesystemService to create filesystems dynamically based on runtime conditions (e.g., tenant-specific storage):
$filesystem = $this->filesystemService->getFilesystem('tenant_' . $tenantId);
Fallback Filesystems:
knp_gaufrette.yaml:
knp_gaufrette:
filesystems:
primary:
adapter: s3
options: { ... }
fallback:
adapter: local
options: { directory: '%kernel.project_dir%/var/fallback' }
FilesystemService::getFilesystem('fallback').Testing:
gaufrette/adapters-test for unit tests to avoid hitting real storage:
test_filesystem:
adapter: test
options: { directory: '%kernel.cache_dir%/test_files' }
How can I help you explore Laravel packages today?