1

I want to upload image files in a table. So I tried this code for uploading image

$fieldset->addField('image', 'file', array(
          'label'     => Mage::helper('menu')->__('Image'),
          'class'     => 'required-entry',
          'required'  => true,
          'name'      => 'image',
      ));

This is my controller file for saving data

public function saveAction() {
        if ($data = $this->getRequest()->getPost()) {

            $model = Mage::getModel('menu/menu');
            $model->setData($data)
            ->setId($this->getRequest()->getParam('id'));

            try {

                $model->save();
                $menu_id = $model->getId();
                if(isset($data['links'])){
                    $customers = Mage::helper('adminhtml/js')->decodeGridSerializedInput($data['links']['customers']); //Save the array to your database

                    $collection = Mage::getModel('menu/grid')->getCollection();
                    $collection->addFieldToFilter('menu_id',$menu_id);
                    foreach($collection as $obj){
                        $obj->delete();
                    }
                    foreach($customers as $key => $value){
                        $model2 = Mage::getModel('menu/grid');
                        $model2->setMenuId($menu_id);
                        $model2->setCustomerId($key);
                        $model2->setPosition($value['position']);
                        $model2->save();
                    }
                }

                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('menu')->__('Item was successfully saved'));
                Mage::getSingleton('adminhtml/session')->setFormData(false);

                if ($this->getRequest()->getParam('back')) {
                    $this->_redirect('*/*/edit', array('id' => $model->getId()));
                    return;
                }
                $this->_redirect('*/*/');
                return;
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                Mage::getSingleton('adminhtml/session')->setFormData($data);
                $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
                return;
            }
        }
        Mage::getSingleton('adminhtml/session')->addError(Mage::helper('menu')->__('Unable to find item to save'));
        $this->_redirect('*/*/');
    }

Ignore menu/grid model, my model is menu/menu

But I am not able to upload it. How can I do it? Thanks

  • 2
    You will require to add image upload code in your controller file. Please share your controller file code – Jaimin Sutariya Apr 10 '17 at 12:44
  • edited @JaiminSutariya – Birju Apr 10 '17 at 12:53
2

Update your field code with below.

$fieldset->addField('image', 'image', array(
          'label'     => Mage::helper('menu')->__('Image'),
          'class'     => 'required-entry',
          'required'  => true,
          'name'      => 'image',
      ));

Use below code in your controller file.

public function saveAction() {
    if ($data = $this->getRequest()->getPost()) {
        if (isset($_FILES['image']['name']) && $_FILES['image']['name'] != '') {
                try {
                    $uploader = new Varien_File_Uploader('image');
                    $uploader->setAllowedExtensions(array('jpg', 'jpeg', 'gif', 'png'));
                    $uploader->setAllowRenameFiles(true);
                    $uploader->setFilesDispersion(true);                   
                    $newDir = "YOUR_DIRECTORY_TO_UPLOAD";

                    $newdirPath = Mage::getBaseDir('media') . DS . "YOUR_DIRECTORY_TO_UPLOAD";

                    if (!file_exists($newdirPath)) {
                        mkdir($newdirPath, 0777);
                    }

                    $path = Mage::getBaseDir('media') . DS . $newDir . DS;
                    $resizedPath = Mage::getBaseDir('media') . DS . $newDir;
                    $uploader->save($path, $_FILES['image']['name']);
                    $uploadedimage = $uploader->getUploadedimage();
                    $_imgUrl = $resizedPath . $uploadedimage;

                } catch (Exception $e) {

                }
                $data['image'] = $uploader->getUploadedimage();
            }
        $model = Mage::getModel('menu/menu');
        $model->setData($data)
        ->setId($this->getRequest()->getParam('id'));

        try {

            $model->save();
            $menu_id = $model->getId();
            if(isset($data['links'])){
                $customers = Mage::helper('adminhtml/js')->decodeGridSerializedInput($data['links']['customers']); //Save the array to your database

                $collection = Mage::getModel('menu/grid')->getCollection();
                $collection->addFieldToFilter('menu_id',$menu_id);
                foreach($collection as $obj){
                    $obj->delete();
                }
                foreach($customers as $key => $value){
                    $model2 = Mage::getModel('menu/grid');
                    $model2->setMenuId($menu_id);
                    $model2->setCustomerId($key);
                    $model2->setPosition($value['position']);
                    $model2->save();
                }
            }

            Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('menu')->__('Item was successfully saved'));
            Mage::getSingleton('adminhtml/session')->setFormData(false);

            if ($this->getRequest()->getParam('back')) {
                $this->_redirect('*/*/edit', array('id' => $model->getId()));
                return;
            }
            $this->_redirect('*/*/');
            return;
        } catch (Exception $e) {
            Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
            Mage::getSingleton('adminhtml/session')->setFormData($data);
            $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
            return;
        }
    }
    Mage::getSingleton('adminhtml/session')->addError(Mage::helper('menu')->__('Unable to find item to save'));
    $this->_redirect('*/*/');
}

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.