FileManagerSystem is a Symfony bundle that provides easy and intuitive management of files and directories: creation, deletion, moving, MIME type handling, image resizing, and more.
It is designed to simplify file management within any Symfony application.
This bundle is stateful: it maintains a navigation context (defaultDirectory, relativeDirectory) across operations.
In the V2 modular architecture, this state is centralized in PathManager and shared across all 6 specialized managers within a single FileManagerService instance (so calling setDefaultDirectory() immediately propagates to file, directory, image, and archive operations).
Note
- Standard PHP-FPM:
shared: falseonFileManagerService(with a sharedPathManagerper request) ensures clean state isolation. - Worker runtimes (FrankenPHP / Swoole): implement
Symfony\Contracts\Service\ResetInterfaceonPathManagerto reset paths between requests.
To keep the codebase maintainable and adhere to the Single Responsibility Principle (SRP), the bundle follows a Facade pattern (FileManagerService / _FileManagerService) delegating to 6 specialized managers located in src/Service/Manager/:
| Manager | Responsibility |
|---|---|
PathManager |
Holds current working directories (kernelDirectory, defaultDirectory, relativeDirectory) and resolves/validates paths. |
MimeManager |
Centralizes EXTENSIONS / MIME_TYPES mappings and guesses real file MIME types on disk. |
FileManager |
Local/remote file reading, file creation, uploads, listing (getFiles), copy, move, rename, delete, and slug generation. |
DirectoryManager |
Directory creation (+, /), listing (getDirs), recursive tree (getDirsTree), Symfony ChoiceType flattening (buildDirectoriesMap), and empty folder cleanup (cleanDir). |
ImageManager |
Image dimensions, GD resizing (JPEG/PNG/WEBP), file size calculation (getSize), and human-readable formatting (getSizeName). |
ArchiveManager |
Single file or bulk downloads (download, downloadBulk) with automatic temporary ZIP creation and post-send cleanup. |
Dependency graph (strictly acyclic β zero circular dependencies):
FileManagerService (Facade)
βββ PathManager (core state β no internal dependencies)
βββ MimeManager βββΊ PathManager
βββ ArchiveManager βββΊ PathManager
βββ ImageManager βββΊ PathManager, MimeManager
βββ FileManager βββΊ PathManager, MimeManager, ImageManager
βββ DirectoryManager βββΊ PathManager, FileManager, ImageManager
Tip
Switching to V2: src/Service/_FileManagerService.php exposes 100% of the existing public API (identical method names, parameter names, and return types). To activate it, replace src/Service/FileManagerService.php with _FileManagerService.php (renaming the class to FileManagerService) and enable the ## V2 block in config/services.yaml.
Install the bundle via Composer:
composer require anfallnorr/file-manager-systemAdd the bundle to your config/bundles.php file:
return [
// ...
Anfallnorr\FileManagerSystem\FileManagerSystem::class => ['all' => true],
];Warning
If you want to use the built-in controller and assets provided by the bundle, create the following configuration files.
Create config/packages/file_manager_system.yaml:
framework:
asset_mapper:
paths:
- '%kernel.project_dir%/vendor/anfallnorr/file-manager-system/assets'Create config/routes/file_manager_system.yaml:
file_manager_system:
resource: '../../vendor/anfallnorr/file-manager-system/src/Controller/'
type: attribute
prefix: /files-manager Inject the FileManagerService into your controller or service:
public function __construct(
private FileManagerService $fmService
) {
$this->fmService->setDefaultDirectory(directory: '/var/uploads');
}For convenience in examples below:
$fmService = $this->fmService;$defaultDirectory = $fmService->getDefaultDirectory();
// Returns: /path/to/project/public/uploads$relativeDirectory = $fmService->getRelativeDirectory();
// Returns: /uploads$directory = $fmService
->setDefaultDirectory(directory: '/var/uploads')
->getDefaultDirectory();
// Returns: /path/to/project/var/uploadsgetDirs(string $path = '/', string $excludeDir = '', string|array|null $depth = '== 0'): array
Explore the file system with support for exclusions, depth control, and relative paths.
$dirs = $fmService->getDirs(path: 'uploads', depth: '== 1');getDirsTree(string $path = '/', string $excludeDir = ""): array
Retrieve directories in a recursive tree structure, including sub-folders and files metadata.
$tree = $fmService->getDirsTree(path: 'uploads');buildDirectoriesMap(array $directories, int $depth = 1, bool $buildDirectoryChoices = false): array
Flatten a recursive directory tree (from getDirsTree()) and optionally build an indented array ready for Symfony ChoiceType.
$result = $fmService->buildDirectoriesMap(directories: $tree, buildDirectoryChoices: true);
$choices = $result['choicesBuilder'];hasDir(): bool
Check if the default directory contains at least one sub-directory.
if ($fmService->hasDir()) {
// Contains sub-directories
}createDir(?string $directory = null, bool $returnDetails = false): array|bool
Create a new directory within the default directory (slugified automatically), or initialize the default directory when null. Supports nested (folder/sub) and multiple (folder1+folder2) creations.
$fmService->createDir(directory: 'Hello World!'); // Creates: hello-worldcleanDir(?string $dir = null): void
Recursively clean a directory by removing empty folders.
$fmService->cleanDir(dir: 'uploads/temp');getFiles(string $path = '/', string|array|null $depth = '== 0', ?string $folder = null, string|array|null $ext = null): array
Offers complete control over file search: depth, extension, folder filtering.
$files = $fmService->getFiles(path: 'uploads', ext: 'jpg');getFileContent(string $relativeFile): string
Read and return the entire content of a file.
$content = $fmService->getFileContent(relativeFile: 'storage/data.json');getRemoteFileContent(string $url): string
Fetch content from a remote URL.
$content = $fmService->getRemoteFileContent(url: 'https://example.com/data.json');createFile(string $filename, string $content = '...'): void
Create a new file with optional content.
$fmService->createFile(filename: 'welcome.html', content: '<h1>Hello</h1>');upload(UploadedFile|File|array $files, string $folder, string $newName = '', bool $returnDetails = false): array|bool
Upload files, handle slugification, and generate useful metadata.
$uploaded = $fmService->upload(files: $file, folder: '/var/www/uploads', newName: 'my-file', returnDetails: true);exists(?string $filePath = null): bool
Check if a file or directory physically exists.
if ($fmService->exists(filePath: 'images/photo.jpg')) {
// File exists
}copy(string $source, string $destination, bool $override = false): bool
Duplicate a file or directory.
$fmService->copy(source: 'uploads/file.txt', destination: 'backup/file.txt', override: true);move(string $origine, string $target, bool $overwrite = false): bool
Move a file or directory to a new location.
$fmService->move(origine: '/uploads/temp/image.jpg', target: '/uploads/final/image.jpg', overwrite: true);rename(string $source, string $destination, bool $override = false): bool
Rename a file or directory (automatically slugifies the new name).
$fmService->rename(source: 'Photo Vacances.jpg', destination: 'nouvelle photo');
// Result: nouvelle-photo.jpgremove(string $relativePath = ''): bool
Delete a file or a directory (and all its content).
$fmService->remove(relativePath: 'uploads/documents/file.txt'); // Delete specific file
$fmService->remove(); // Delete entire default directoryresizeImages(array $files, string $sourceDir, string $targetDir, int $width, int $quality = 100, ?string $suffix = null): array
Resize images while keeping aspect ratio and saving them to a new destination.
$fmService->resizeImages(files: ['img.jpg'], sourceDir: '/source', targetDir: '/target', width: 800, quality: 90, suffix: 'thumb');getImageSize(string $filePath): ?array
Get the dimensions of an image (['width' => int, 'height' => int]).
$size = $fmService->getImageSize(filePath: 'uploads/photo.jpg');getSize(string|array $files, int $totalFileSize = 0): int|float
Calculate the total size in bytes of one or more files.
$bytes = $fmService->getSize(files: $filesArray);getSizeName(int|float $size): string
Convert bytes into a human-readable format (Octets, Ko, Mo, Go).
$readable = $fmService->getSizeName(size: 10485760); // 10 MogetMimeTypes(): array
Get a complete list of supported MIME types.
$mimeTypes = $fmService->getMimeTypes();getMimeType(string $key): string|array|null
Retrieve the MIME type for a given file extension.
$mimeType = $fmService->getMimeType(key: 'pdf');getMimeContent(string $filename): ?string
Detect the real MIME type of a physical file based on its content.
$mimeContent = $fmService->getMimeContent(filename: 'uploads/photo.jpg');download(string $name, ?string $directory = null): BinaryFileResponse
Force the download of a single file, or automatically compress and download a folder as a ZIP archive.
return $fmService->download(name: 'document.pdf');downloadBulk(array $names, ?string $directory = null): BinaryFileResponse
Group multiple files into a ZIP archive and force its download.
return $fmService->downloadBulk(names: ['doc1.pdf', 'img.png']);createSlug(string $string): string
Convert any string into a URL-safe slug.
$slug = $fmService->createSlug(string: 'Hello World !'); // hello-worldIf you are using Twig and want Bootstrap-styled forms, add the following to your Twig configuration.
Edit config/packages/twig.yaml:
twig:
form_themes: ['bootstrap_5_layout.html.twig']This bundle is open-source and available under the MIT License.