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
4 Answers 4
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('*/*/');
}
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 alsoDevelop– Develop2017年03月20日 04:22:59 +00:00Commented Mar 20, 2017 at 4:22
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– Develop2017年03月20日 06:34:26 +00:00Commented Mar 20, 2017 at 6:34
-
provide your addFeild code2017年03月20日 06:42:08 +00:00Commented Mar 20, 2017 at 6:42
-
$fieldset->addField('imageurl', 'image', array( 'label' => Mage::helper('gallery')->__('Image'), 'name' => 'imageurl', 'required' => true, 'note' => '(*.jpg, *.png, *.gif)', ));Develop– Develop2017年03月20日 06:57:21 +00:00Commented Mar 20, 2017 at 6:57
-
@Develop check Edit part2017年03月20日 07:01:38 +00:00Commented Mar 20, 2017 at 7:01
-
File was not uploaded. i got this errorDevelop– Develop2017年03月20日 07:14:26 +00:00Commented Mar 20, 2017 at 7:14
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',
)
);