composer require akyos/file-manager-bundle
config/bundles.php:
return [
// ...
Akyos\FileManagerBundle\AkyosFileManagerBundle::class => ['all' => true],
];
config/packages/akyos_file_manager.yaml (or create it):
akyos_file_manager:
storage:
path: '%kernel.project_dir%/public/uploads' # Default upload directory
adapter: 'local' # Options: 'local', 's3', 'gcs'
# S3/GCS config (if applicable)
# aws:
# key: '%env(AWS_KEY)%'
# secret: '%env(AWS_SECRET)%'
# bucket: 'my-bucket'
FileManager service in a controller:
use Akyos\FileManagerBundle\Service\FileManager;
public function uploadFile(Request $request, FileManager $fileManager)
{
$file = $request->file('file');
$path = $fileManager->upload($file, 'user_uploads'); // 'user_uploads' = subdirectory
return response()->json(['path' => $path]);
}
src/Service/FileManager.php – Core methods (upload, delete, getUrl, listFiles).config/packages/akyos_file_manager.yaml – Storage backends and defaults.src/Twig/FileManagerExtension.php – For template-based file operations (e.g., {{ file_manager.getUrl(file) }}).use Symfony\Component\HttpFoundation\File\UploadedFile;
public function handleUpload(UploadedFile $file, FileManager $fileManager)
{
// Validate file type/size
if (!$file->isValid() || $file->getClientOriginalExtension() !== 'pdf') {
throw new \RuntimeException('Invalid file');
}
// Upload with custom path
$path = $fileManager->upload($file, 'documents/' . uniqid());
return $this->redirectToRoute('download', ['path' => $path]);
}
// In a controller or Twig template
$url = $fileManager->getUrl('documents/report.pdf'); // Returns full public URL
$files = $fileManager->listFiles('user_uploads');
foreach ($files as $file) {
echo $file->getPath(); // e.g., 'user_uploads/123.pdf'
}
$fileManager->delete('user_uploads/123.pdf');
FileType with custom validation:
$builder->add('file', FileType::class, [
'mapped' => false,
'constraints' => [new File(['maxSize' => '1024k', 'mimeTypes' => ['application/pdf']])],
]);
EventDispatcher integration).return $this->json(['downloadUrl' => $fileManager->getUrl($path)]);
Permissions Issues
storage.path directory is writable by the web server:
chmod -R 775 %kernel.project_dir%/public/uploads
Missing Configuration
s3/gcs, all required env vars must be set (e.g., AWS_KEY, AWS_SECRET). The bundle does not fall back to defaults.Relative vs. Absolute Paths
getUrl() returns a public URL (e.g., /uploads/file.pdf). Use getAbsolutePath() for filesystem operations:
$absolutePath = $fileManager->getAbsolutePath('file.pdf');
No Built-in Thumbnails
intervention/image) if needed.APP_DEBUG=1) to see storage adapter errors.if (!$fileManager->exists('file.pdf')) {
throw new \RuntimeException('File not found');
}
local adapter before migrating to S3/GCS.Custom Storage Adapters
Akyos\FileManagerBundle\Storage\StorageInterface to add support for new backends (e.g., Azure Blob).File Naming Strategies
uuid()) by extending FileManager and injecting a custom FileNamer service.Events (If Supported)
file.uploaded or file.deleted events (check the bundle’s Event classes for details).Twig Filters
FileManagerExtension to add custom Twig filters (e.g., {{ file_manager.getThumbnailUrl(file) }}).users/{id}/profile.jpg).getUrl() results in cache if files are static.listFiles() + delete()).How can I help you explore Laravel packages today?