How to upload product image in Magneto 2

Uploading the product image in Magento 2 via the code snippet is the helpful article for the developers. Using the picture is a close way to introduce your products to the customers in the shopping online. As you know, a huge wonder making the buyer consider carefully to buy is what that looks like. The photo with the major details will answer the question as well as doesn’t require the buyers to imagine in their minds.

So what do you need to upload the product image in Magento 2 from controller file? Here are snippet:

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Backend\App\Action;
 
protected $_fileUploaderFactory;
 
public function __construct(
    \Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory,
    Action\Context $context
      
) {
 
    $this->_fileUploaderFactory = $fileUploaderFactory;
    parent::__construct($context);
}
 
public function execute(){
 
    $uploader = $this->_fileUploaderFactory->create(['fileId' => 'image']);
      
    $uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
      
    $uploader->setAllowRenameFiles(false);
      
    $uploader->setFilesDispersion(false);
 
    $path = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)
      
    ->getAbsolutePath('images/');
      
    $uploader->save($path);
 
}

That is it! If that you have any queries about the article or any questions in general, use the comment section below!



Next tutorial:

Module Development Series



Enjoyed the tutorial? Spread it to your friends!

magento-2-tutorial
how-to
upload
product
image

Module Development

Comments for How to upload product image in Magneto 2

Please leave comments if you have any questions, feedbacks.

You also may like these Magento 2 Extensions

One Step Checkout

$199
10 reviews

Layered Navigation

$99
12 reviews

SEO

FREE
no review

Shop By Brand

$99
3 reviews

People also searched for:

  • magento 2 upload product image
  • magento 2 how to upload image
  • upload image magento 2
  • upload image in magento 2
  • upload product image in magento 2
  • upload product image magento 2