0

Magento Save Custom Image In Backend

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

            $postData = $this->getRequest()->getPost();
            $uploader = new Varien_File_Uploader('img');
            $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png')); // Your Format Here
            $uploader->setAllowRenameFiles(true);
            $uploader->setFilesDispersion(false);
            $path = Mage::getBaseDir('media').DS.'Gallery'.DS;
            $uploader->save($path, $_FILES['img']['name']);
            $img = $_FILES['img']['name'];
            $galleryModel = Mage::getModel('gallery/gallery');                
            $galleryModel->setId($this->getRequest()->getParam('id'))
                ->setTitle($postData['title'])
                ->setDescription($postData['description'])
                ->setStatus($postData['status'])
                ->setImageurl($postData['imageurl'])
                ->save();

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

            $this->_redirect('*/*/');
            return;
        } catch (Exception $e) {
            Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
            Mage::getSingleton('adminhtml/session')->setgalleryData($this->getRequest()->getPost());
            $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
            return;
        }
    }
    $this->_redirect('*/*/');
}

I got below this error

enter image description here

0

Try this,

make sure before you need to create folder media/Gallery

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

                $postData = $this->getRequest()->getPost();
                $uploader = new Varien_File_Uploader('imageurl');
                $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png')); // Your Format Here
                $uploader->setAllowRenameFiles(true);
                $uploader->setFilesDispersion(false);
                $path = Mage::getBaseDir('media').DS.'Gallery'.DS;                
                $img = $path. $_FILES['imageurl']['name'];
                $filename = $uploader->getNewFileName($img);
                $uploader->save($path, $filename);
                $galleryModel = Mage::getModel('gallery/gallery');                
                $galleryModel->setId($this->getRequest()->getParam('id'))
                    ->setTitle($postData['title'])
                    ->setDescription($postData['description'])
                    ->setStatus($postData['status'])
                    ->setImageurl($filename)
                    ->save();
                //var_dump($uploader);exit();

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

                $this->_redirect('*/*/');
                return;
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                Mage::getSingleton('adminhtml/session')->setgalleryData($this->getRequest()->getPost());
                $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
                return;
            }
        }
        $this->_redirect('*/*/');
    }
0

Looks like you posted the form without uploading an image. Make sure that the form has the enctype='multipart/form-data' attribute.

  • I need to create admin phtml also – Develop Mar 20 '17 at 4:22
0

As Fabian says and what I understand as per your screenshot you need to add enctype='multipart/form-data' for that in your Form.php of your module add below code.

 $form = new Varien_Data_Form(array(
        'id' => 'edit_form',
        'action' => $this->getUrl(
            'adminhtml/controller/action',
            array(
                '_current' => true,
                'continue' => 0,
            )
        ),
        'method' => 'post',
        'enctype' => 'multipart/form-data' //add this line 
    ));

EDIT

 $fieldset->addField('file', 'file', array(
        'label'     => Mage::helper('gallery')->__('Image'),
        'class'     => 'disable',
        'required'  => true,
        'name'      => 'imageurl',
    ));
  • I changed that also still not upload I think I missed something in savefunction() – Develop Mar 20 '17 at 6:34
  • provide your addFeild code – Murtuza Zabuawala Mar 20 '17 at 6:42
  • $fieldset->addField('imageurl', 'image', array( 'label' => Mage::helper('gallery')->__('Image'), 'name' => 'imageurl', 'required' => true, 'note' => '(*.jpg, *.png, *.gif)', )); – Develop Mar 20 '17 at 6:57
  • @Develop check Edit part – Murtuza Zabuawala Mar 20 '17 at 7:01
  • File was not uploaded. i got this error – Develop Mar 20 '17 at 7:14
0

The issue happen with me. I fixed the issue by edit code on block form (example: app/code/local/Leric/Blog/Block/Manage/Blog/Edit/Form.php)

Add 'enctype' => 'multipart/form-data' to form:

$form = new Varien_Data_Form(
        array(
             'id'     => 'edit_form',
             'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))),
             'method' => 'post',
             'enctype' => 'multipart/form-data',
        )
    );

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.