如何在Magento 2中获取文件夹和子文件夹中的所有文件

5
我制作了一个用于在前端上传图像的模块。Magento 2以一种特殊的方式保存文件。例如:
  • 上传文件 - file.png,
  • 文件路径 - pub/media/[module_folder]/f/i/file.png
如何获取[module_folder]中的所有文件? enter image description here

如何获取它们?使用什么系统/语言?以什么格式? - Jamie Cockburn
我想获取文件夹和子文件夹中所有文件的名称和路径。 Magento 2,PHP - Alex
1个回答

6

尝试以下操作,使用directorylist类获取路径,使用file类读取目录: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;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接