FileManagerSystem is a Symfony bundle to easily manage files and directories: creation, deletion, moving, resizing images, managing MIME types, etc.
Install FileManagerSystem via Composer
composer require anfallnorr/file-manager-systemRegister the bundle in config/bundles.php
# config/bundles.php
return [
...
Anfallnorr\FileManagerSystem\FileManagerSystem::class => ['all' => true],
];Create config/packages/file_manager_system.yaml
# config/packages/file_manager_system.yaml
framework:
asset_mapper:
paths:
- '%kernel.project_dir%/vendor/anfallnorr/file-manager-system/assets'public function __construct(
private FileManagerService $fmService
) {
$fmService
->setDefaultDirectory('/var/uploads')
->setRelativeDirectory('/var/uploads');
}$fmService = $this->fmService;// Get the default upload directory path
$defaultDirectory = $fmService->getDefaultDirectory(); // /path/to/folder/public/uploads
// Change the default upload directory
$directory = $fmService->setDefaultDirectory(directory: '/var/www/uploads')->getDefaultDirectory(); // /path/to/folder/var/www/uploads
// Retrieve available MIME types
$mimeTypes = $fmService->getMimeTypes(); // array
// Get the MIME type of a specific extension
$mimeType = $fmService->getMimeType(key: 'pdf'); // application/pdf
// Create a URL-friendly slug from a string
$string = $fmService->createSlug(string: 'Hello World !'); // hello-world
// Create a directory named "hello-world" inside the default directory
$fmService->createDir(directory: 'Hello World !', return: false);
// if $return is `true`, then an array will be returned:
[
'absolute' => $this->getDefaultDirectory() . '/hello-world', // Absolute path
'relative' => $relative, // Relative path of the folder
'ltrimed_relative' => ltrim($relative, '/'), // Relative path of the folder minus a slash at the beginning of the string
'foldername' => $dir // The name of the folder created
]
// Create a file named "hello-world.html" inside the default directory with content
$fmService->createFile(filename: 'Hello World.html', content: 'Hello World! I\'m Js info'); // $content is optional
// This method allows you to download a file located in a specified directory (or in the default directory if none is provided).
// It checks for the file's existence and returns a BinaryFileResponse configured to force the download.
$fmService->download(filename: $filename, directory: $folder);
// This method generates a ZIP file containing a list of files from a given directory (or the default directory).
// It checks for the existence of each file, temporarily constructs the ZIP, and returns a BinaryFileResponse allowing the file to be downloaded.
$fmService->downloadBulk(filenames: $files, folders: $folders);If you are using Twig, add Bootstrap form themes in config/packages/twig.yaml
# app/config/packages/twig.yaml
twig:
form_themes: ['bootstrap_5_layout.html.twig']