How to upload product image in Magento 2

Today’s post will provide a helpful tutorial about uploading product images in Magento 2. It’s easy for developers of any level to follow successfully.

Why product image is important?

Product images play a vital role in marketing your products. Needless to say, visual elements are alway more pleasing to eyes than text. So customers, when they first see your products, will land their eyes on the product images rather than reading product description. That’s the biggest reason why you should not ignore product images.

This directly boost your website’s visual attraction and enhance your customer experience.

Upload product image in Magento 2 via code snippet

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;
public function execute(){
    $uploader = $this->_fileUploaderFactory->create(['fileId' => 'image']);
    $uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
    $path = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)

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

Related Topics

Enjoyed the tutorial? Spread it to your friends!


Sam Thomas
Sam Thomas

Sam is the CEO and Founder of Mageplaza, pursuing a simple and healthy lifestyle. He is a friend, a husband, and a father to two children. As a trainer and an aspiring influencer, he is also a big fan of sports and travel. Sam is exceptionally knowledgeable about e-commerce, especially Magento and Shopify, with 10 years of experience in the field.

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
  • 2.2.x, 2.3.x, 2.4.x