I am new to Magento and i want upload upload product programmatically along with product image, i am using this code to implement, please check

$_product->setSku($product["sku"]); // this must be unique everytime and get the exception in frontend

//$imagePath = $product['imgpath'];

$_product->setMediaGallery(array('images'=>array (),'values'=>array ())); 
$_product->addImageToMediaGallery('https://beebom-redkapmedia.netdna-ssl.com/wp-content/uploads/2016/01/Reverse-Image-Search-Engines-Apps-And-Its-Uses-2016.jpg', array('image', 'small_image', 'thumbnail'),false);
// $_product->setImage('https://beebom-redkapmedia.netdna-ssl.com/wp-content/uploads/2016/01/Reverse-Image-Search-Engines-Apps-And-Its-Uses-2016.jpg');

// $_product->setSmallImage('/testimg/test.jpg');

// $_product->setThumbnail('/testimg/test.jpg');
        'use_config_manage_stock' => 0, //'Use config settings' checkbox
        '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 provided the static path for the image and its opening in the browser, but getting error The image does not exist. while uploading product. please suggest.

1 Answer 1


The 1st parameter of addImageToMediaGallery is the local path of the image, NOT remote path. Of course we can have some tweaks. Just download the images to a temporary folder, then add them into the product gallery.

/** @var string $tmpDir */
$tmpDir = $this->directoryList->getPath(DirectoryList::MEDIA) . DIRECTORY_SEPARATOR . 'tmp';
/** create folder if it is not exists */
/** @var string $newFileName */
$newFileName = $tmpDir . baseName($imageUrl);
/** read file from URL and copy it to the new destination */
$result = $this->file->read($imageUrl, $newFileName);
if ($result) {
    /** add saved file to the $product gallery */
    $_product->addImageToMediaGallery($newFileName, $imageType, true, $visible);

Subsitute $imageUrl into your image remote URL. You should inject \Magento\Framework\App\Filesystem\DirectoryList in order to use $this->directoryList and get constant DirectoryList::MEDIA. Also, you need to inject \Magento\Framework\Filesystem\Io\File to use $this->file.

  • Fatal error: Allowed memory size of 805306368 bytes exhausted (tried to allocate 262144 bytes)
    – Jay Kapoor
    Aug 14, 2017 at 8:35
  • got this errorr
    – Jay Kapoor
    Aug 14, 2017 at 8:36
  • what if you provide me a way to upload the file from local not from the remote URL , i just want to upload image thats all, thanks
    – Jay Kapoor
    Aug 14, 2017 at 8:42
  • how can get $ImageUrl when choose image i try : $imageUrl=$_FILES['imageUploader']['name']; But not read the file
    – HaFiz Umer
    Jul 15, 2019 at 11:05

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.