I want to upload a product along with product image, please suggest how to set path or upload image for a product. here is my code


                $tmpDir = $this->getMediaDirTmpDir();
                $newFileName = $tmpDir . baseName($imageUrl);
                $result = $this->file->read($imageUrl, $newFileName);
                 if ($result) {
                /** add saved file to the $product gallery */
                $_product->setMediaGallery(array('images'=>array (),'values'=>array ())); 
                $_product->addImageToMediaGallery(how to set path, what to do,array('image', 'small_image', 'thumbnail'), true, $visible);

         'manage_stock' => 1, //manage stock
   'min_sale_qty' => 1, //Minimum Qty Allowed in Shopping Cart
                        'max_sale_qty' => 2, //Maximum Qty Allowed in Shopping Cart
                        'is_in_stock' => $product['stockstatus'], //Stock Availability
                        'qty' =>  $product['quantity']

            catch(\Exception $e) { 
                 echo $e->getMessage();
  • i am suggesting you please try this in test script from root folder and hard code the image path first, if it works then do it dynamically next stackoverflow.com/questions/40259303/… Aug 14, 2017 at 9:36
  • i did but always got image does not exist this error
    – Jay Kapoor
    Aug 14, 2017 at 9:44

3 Answers 3


Simply add this code after product price (No need another code for upload image)

 $imagePath = "D:\image\sample.jpg"; // path of the image
 $_product->addImageToMediaGallery($imagePath, array('image', 'small_image', 'thumbnail'), true, false);
  • is the same path of all the words system ?
    – HaFiz Umer
    Jul 15, 2019 at 12:27
  • no luck @israt :( Dec 28, 2020 at 14:59
 use Magento\Framework\App\Bootstrap;
 require __DIR__ . '/app/bootstrap.php';

 $bootstrap = Bootstrap::create(BP, $_SERVER);

 $obj = $bootstrap->getObjectManager();

 $state = $obj->get('Magento\Framework\App\State');

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  $importDir = __DIR__ . '/pub/media/catalog/product'; //This is the directory path from where you have to take the images
  $i = '12'; // It must be product ID for which product it is to be assigned
  $product = $objectManager->get('Magento\Catalog\Model\Product')->load($i);
  $id = $product->getId();
  $url = $importDir . $product->getImage();
  $product->addImageToMediaGallery($url, array('image', 'small_image', 'thumbnail'), true, false);
  echo "<br /><br /> $id Product Save Succefully";
 catch(\Exception $e)
  echo $e->getMessage();
foreach ($productImages as $key => $productImage) {
    if ($key === array_key_first($productImages)) {
        $product->addImageToMediaGallery($productImage, array('image', 'small_image', 'thumbnail'), true, false);
    $product->addImageToMediaGallery($productImage, [], true, false);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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