我制作了一个用于在前端上传图像的模块。 Magento 2以特殊方式保存文件。例如:
file.png
,pub/media/[module_folder]/f/i/file.png
。如何从[module_folder]
获取所有文件?
答案 0 :(得分:1)
尝试以下操作,使用directorylist类获取路径,并使用文件类读取目录: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;
}