如何从magento 2中的文件夹和子文件夹中获取所有文件

时间:2016-02-16 09:52:27

标签: filesystems magento2 getfiles

我制作了一个用于在前端上传图像的模块。 Magento 2以特殊方式保存文件。例如:

  • 上传文件 - file.png
  • 文件路径 - pub/media/[module_folder]/f/i/file.png

如何从[module_folder]获取所有文件?

enter image description here

1 个答案:

答案 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;
}