Hello Guys I am creating product programmatically from frontend by submitting a form but when I am doing :


I am not getting the image data in array.

Also I want to add that in product how I can do that please guide me.

here the code of my frontend form :

<form action="<?php echo $this->getUrl('custom_abc/customProduct/addProduct') ?>" method="post" name="add-product" autocomplete="off" id="add-product-form" class="scaffold-form" enctype="multipart/form-data">
    <div class="field product-image">
        <label for="product-image" class="required"><em>*</em>Product Image</label>
        <div class="input-box">
            <input type="file" id="product-image" name="product-image" title="Product Image" class="input-text required-entry">

Here is the code of my product addition :


$product = Mage::getModel('catalog/product')->load($productData['productId']);

try {
                ->setMediaGallery(array('images' => array(), 'values' => array())) //media gallery initialization
                ->addImageToMediaGallery('not understanding what to do here', array('image', 'thumbnail', 'small_image'), false, false) //assigning image, thumb and small image to media gallery

        if ($product->save()) {

            Mage::getSingleton('core/session')->addSuccess('Your Product Uploaded Successfully');

            Mage::getSingleton('core/session')->addError('Error in uploading Product');
    } catch (Exception $e) {

Here is the working example

if(file_exists(Mage::getBaseDir('media') . DS . 'import' . DS . $gallery_img)){
        $product->addImageToMediaGallery(Mage::getBaseDir('media') . DS . 'import' . DS . $gallery_img, null, false, false);

As you can see here, I have used full image path to the place where you have placed not understanding what to do here. Add your full imagepath to it.

| improve this answer | |
<input type="hidden" name="form_key" value="<?php echo Mage::getSingleton('core/session')->getFormKey(); ?>" />

You must have to include form_key field in the form. This will allow you to submit a form.

| improve this answer | |

You won't get image data in $this->getRequest()->getPost(); You need to use $_FILES for that. Once you get your uploaded filepath just replace it in the code @rohit suggested.

| improve this answer | |

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.