I made a module for uploading images in frontend. Magento 2 saves files in a special way. For example:
- uploading file -
file.png
, - path to file -
pub/media/[module_folder]/f/i/file.png
.
How to get all files from [module_folder]
?
I made a module for uploading images in frontend. Magento 2 saves files in a special way. For example:
file.png
,pub/media/[module_folder]/f/i/file.png
.How to get all files from [module_folder]
?
Try the below, use the directorylist class to get the path, and the file class to read the directory :D
public function __construct(
\Magento\Framework\Filesystem\DirectoryList $directoryList,
\Magento\Framework\Filesystem\Driver\File $driverFile,
LoggerInterface $logger)
{
$this->directoryList =$directoryList;
$this->driverFile = $driverFile;
$this->logger = $logger;
}
public function getAllFiles($path = '/import/') {
$paths = [];
try {
//get the base folder path you want to scan (replace var with pub / media or any other core folder)
$path = $this->directoryList->getPath('var') . $path;
//read just that single directory
$paths = $this->driverFile->readDirectory($path);
//read all folders
$paths = $this->driverFile->readDirectoryRecursively($path);
} catch (FileSystemException $e) {
$this->logger->error($e->getMessage());
}
return $paths;
}
© 2022 - 2024 — McMap. All rights reserved.