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 (e.g. current directory, browsing state) across requests.
The state is securely isolated per user session, ensuring that each user interacts with their own file system context without interference.
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');hasDir(): bool
Check if the default directory contains at least one sub-directory.
if ($fmService->hasDir()) {
// Contains sub-directories
}createDir(string $directory, bool $returnDetails = false): array|bool
Create a new directory within the default directory (slugified automatically). 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 (o, Ko, Mo, Go).
$readable = $fmService->getSizeName(size: 10485760); // 10.00 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.
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.