Как получить все файлы из папки и подпапки в magento 2

Я сделал модуль для загрузки изображений в интерфейсе. Magento 2 сохраняет файлы особым образом. Например:

  • загрузка файла – file.png,
  • путь к файлу - pub/media/[module_folder]/f/i/file.png.

Как получить все файлы из [module_folder]?

введите здесь описание изображения


person Alex    schedule 16.02.2016    source источник
comment
Получить их как? В какой системе/языке? В каком формате?   -  person Jamie Cockburn    schedule 16.02.2016
comment
Я хочу получить имена и пути всех файлов в папке и подпапке. Мадженто 2, PHP   -  person Alex    schedule 16.02.2016


Ответы (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;
}
person TTech IT Solutions    schedule 14.07.2020