1

I want to Get All Product Images From Catalog/Product Images Directory

I am trying this but not getting Success

use Magento\Framework\Filesystem\Io\File;
use Magento\Store\Model\StoreManagerInterface;

class Index
{
    public function __construct(
        StoreManagerInterface $storeManager,
        File $io
    ) {
        $this->storeManager       = $storeManager;
        $this->_io                = $io;
    }
    public function execute()
    {
        $dir = $this->storeManager->getStore()->getBaseUrl(
            \Magento\Framework\UrlInterface::URL_TYPE_MEDIA
        ) . 'catalog/product';
        $this->_io->chmodRecursive($dir, 0777);
        $images = $this->_io->ls($dir);
        echo $dir;
        print_r($images);
        exit();
    }
}
2
  • do you want images for specific product? – Mohit Rane Jul 9 '19 at 11:11
  • no i want all images – Waqar Ali Jul 9 '19 at 11:13
0

Well After Some i Did it my Myself

class Index
{
    public function __construct(
        \Magento\Framework\Filesystem\DirectoryList $dir,
    ) {
           $this->dir                = $dir;
    }
    public function execute()
    {
        $dir = $this->dir->getPath('media') . '/catalog/product';
        $images      = $this->scd($dir);
        echo "<pre>";
        print_r($images);
        exit();
    }
}
public function scd($path)
    {
        $di       = new RecursiveDirectoryIterator($path);
        $iterator = new RecursiveIteratorIterator($di, RecursiveIteratorIterator::CHILD_FIRST);
        $files    = [];
        $allowed  = ['jpg'];
        foreach ($iterator as $file) {
            $ext = pathinfo($file, PATHINFO_EXTENSION);
            if (in_array($ext, $allowed)) {
                //if you want File Name With Path You can Try this
                $files[] = $file->getPathName();
                //if you want File Name Only You can Try this
                $files[] = $file->getFileName();
            }
        }
        return $files;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.