2

I created a form to add products in the frontend, and I've added products with all attributes, except for product image. When I save, it shows: Image does not exist. Please help me find the problem. Thanks.

My template:

<div class="field name-image">
    <label class="required"><em>*</em><?php echo $this->__('Image') ?></label>
    <div class="input-box">
        <input type="file" id="<?php echo $this->getFieldId('image')?>" name="image title="<?php echo Mage::helper('core')->quoteEscape($this->getStoreLabel('image')) ?>" />
    </div>
</div>

In controller:

 $product = new Mage_Catalog_Model_Product();

 $filename = $_FILES['image']['name'];
 $image = Mage::getBaseDir('media') . DS . "catalog\product" . DS . $filename;
 $product ->setMediaGallery(array('images' => array(), 'values' => array()))
            ->addImageToMediaGallery($image, array('image', 'small_image', 'thumbnail'), false, false);
 $product->save();
1

You need to do code like this.

$path = '';

if(isset($_FILES['image']['name']) && $_FILES['image']['name'] != '') 
{
    try 
    {
        $uploader = new Varien_File_Uploader('image');
        $uploader->setAllowedExtensions(array(‘jpg’,’jpeg’,’gif’,’png’));
        $uploader->setAllowRenameFiles(false);
        $uploader->setFilesDispersion(false);

        $path = Mage::getBaseDir(‘media’) . DS . "catalog\product" . DS ;
        $uploader->save($path, $_FILES[‘image’][‘name’] );
     } 
     catch (Exception $e) 
     {
        Mage::log($e->getMessage());
     }

     $product->save();

     $newpath = $path.$_FILES[‘image’][‘name’];
     $product->addImageToMediaGallery($newpath, array('image', 'thumbnail', 'small_image'), false, false);
     $product->save();
}

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.