You can put a var_dump in app/code/core/Mage/Catalog/Model/Product/Attribute/Backend/Media.php
around line 280 and see what do you have inside $pathinfo
.
Code is quite clear, it seems to check the extension of the file you are uploading only.
$pathinfo = pathinfo($file);
$imgExtensions = array('jpg','jpeg','gif','png');
if (!isset($pathinfo['extension']) || !in_array(strtolower($pathinfo['extension']), $imgExtensions)) {
Mage::throwException(Mage::helper('catalog')->__('Invalid image file type.'));
}
First update your Form.php file
$fieldset->addField('iconimage', 'image', array(
'label' => Mage::helper('service')->__('Icon Image'),
'name' => 'image',
'note' => '(*.jpg, *.png, *.gif)',
));
to
$fieldset->addField('iconimage', 'image', array(
'label' => Mage::helper('service')->__('Icon Image'),
'name' => 'iconimage',
'note' => '(*.jpg, *.png, *.gif)',
));
Now update your controller code saveAction()
public function saveAction() {
$post_data = $this->getRequest()->getPost();
if ($post_data) {
try {
//Featured save image
try {
if ((bool) $post_data['image']['delete'] == 1) {
$post_data['image'] = '';
} else {
unset($post_data['image']);
if (isset($_FILES)) {
if ($_FILES['image']['name']) {
if ($this->getRequest()->getParam("id")) {
$model = Mage::getModel("service/service")->load($this->getRequest()->getParam("id"));
if ($model->getData('image')) {
$io = new Varien_Io_File();
$io->rm(Mage::getBaseDir('media') . DS . implode(DS, explode('/', $model->getData('image'))));
}
}
$path = Mage::getBaseDir('media') . DS . 'service' . DS . 'service' . DS;
$uploader = new Varien_File_Uploader('image');
$uploader->setAllowedExtensions(array('jpg', 'png', 'gif'));
$uploader->setAllowRenameFiles(true);
$uploader->setFilesDispersion(false);
$destFile = $path . $_FILES['image']['name'];
$filename = $uploader->getNewFileName($destFile);
$uploader->save($path, $filename);
$post_data['image'] = 'service/service/' . $filename;
}
}
}
} catch (Exception $e) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
$this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
return;
}
//save image
// Icon Image Save
try {
if ((bool) $post_data['iconimage']['delete'] == 1) {
$post_data['iconimage'] = '';
} else {
unset($post_data['iconimage']);
if (isset($_FILES)) {
if ($_FILES['iconimage']['name']) {
if ($this->getRequest()->getParam("id")) {
$model = Mage::getModel("service/service")->load($this->getRequest()->getParam("id"));
if ($model->getData('iconimage')) {
$io = new Varien_Io_File();
$io->rm(Mage::getBaseDir('media') . DS . implode(DS, explode('/', $model->getData('iconimage'))));
}
}
$path = Mage::getBaseDir('media') . DS . 'service' . DS . 'service' . DS;
$uploader = new Varien_File_Uploader('iconimage');
$uploader->setAllowedExtensions(array('jpg', 'png', 'gif'));
$uploader->setAllowRenameFiles(true);
$uploader->setFilesDispersion(false);
$destFile = $path . $_FILES['iconimage']['name'];
$filename = $uploader->getNewFileName($destFile);
$uploader->save($path, $filename);
$post_data['iconimage'] = 'service/service/' . $filename;
}
}
}
} catch (Exception $e) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
$this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
return;
}
$model = Mage::getModel("service/service")
->addData($post_data)
->setId($this->getRequest()->getParam("id"))
->save();
Mage::getSingleton("adminhtml/session")->addSuccess(Mage::helper("adminhtml")->__("Service was successfully saved"));
Mage::getSingleton("adminhtml/session")->setServiceData(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")->setServiceData($this->getRequest()->getPost());
$this->_redirect("*/*/edit", array("id" => $this->getRequest()->getParam("id")));
return;
}
}
$this->_redirect("*/*/");
}
Best Answer
I just edit file path like
instead of
That's All. Now Its working well
For further details please lookout this post