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/…
    – Nagaraju K
    Aug 14 '17 at 9:36
  • i did but always got image does not exist this error
    – Jay Kapoor
    Aug 14 '17 at 9:44

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 '19 at 12:27
  • no luck @israt :( Dec 28 '20 at 14:59

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.