Show thumbnail image and delete image in custom module admin form
Add the code in your controller file
public function saveAction() { ... if (!empty( $_FILES['main_image']['name'] )) { $data['main_image'] = $_FILES['main_image']['name'] ; } else { if (isset($data['main_image']['delete']) && $data['main_image']['delete'] == 1) { if ($data['main_image']['value'] != '') $this->removeFile($data['main_image']['value']); $data['main_image'] = ''; } else { unset($data['main_image']); } } ... } public function removeFile($file) { $_helper = Mage::helper('ram'); $file = $_helper->updateDirSepereator($file); $directory = Mage::getBaseDir('media') . DS .'ram_images' ; $io = new Varien_Io_File(); $result = $io->rmdir($directory, true); }
Create below function in Helper Class
public function updateDirSepereator($path) { return str_replace('\\', DS, $path); }
Add 'enctype'’ ⇒ 'multipart/form-data'
That should help to get something in $_FILES
in /app/code/local/Chilly/ModName/Block/Adminhtml/ModName/Edit/Form.php
You should have something looking like this:
$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'
)
);
Then in /app/code/local/Chilly/ModName/Block/Adminhtml/ModName/Edit/Tab/Form.php
$fieldset->addField('main_image', 'image', array(
'label' => Mage::helper('ram')->__('Image'),
'required' => false,
'name' => 'main_image',
));
Then in /app/code/local/Chilly/ModName/controllers/Adminhtml/ModuleNameController.php in saveAction()
if(isset($_FILES['main_images']['name'])) {
try {
$uploader = new Varien_File_Uploader('main_images');
$uploader->setAllowedExtensions(array('jpg','jpeg','gif','png')); // or pdf or anything
$uploader->setAllowRenameFiles(false);
$uploader->setFilesDispersion(false);
$path = Mage::getBaseDir('media') . DS. 'ram_images'.DS ;
$destFile = $path.$_FILES['main-images']['name'];
$filename = $uploader->getNewFileName($destFile);
$uploader->save($path, $filename);
$data[main_images] = 'ram_images'. $filename;
}catch(Exception $e) { }
}
Your's edit form will be display image like as..
I think i got a simple solution here to display thumbnail image.
if(isset($_FILES['main_images']['name'])) {
try {
$uploader = new Varien_File_Uploader('main_images');
$uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
$uploader->setAllowRenameFiles(false);
$uploader->setFilesDispersion(false);
$path = Mage::getBaseDir('media') . / ;
$uploader->save($path, $_FILES['main_images']['name']);
$data[main_images] ='ram_images'. $_FILES['main_images']['name'];
}catch(Exception $e) { }
}
just saved folder name too in the table and it worked.
To delete image just added following code to module controllers saveAction()
function.
if (!empty( $_FILES['main_image']['name'] ))
{
$data['main_image'] = $_FILES['main_image']['name'] ;
}
else
{
if (isset($data['main_image']['delete']) && $data['main_image']['delete'] == 1)
{
$data['main_image']='';
}
else
{
unset($data['main_iamge']);
}
}
It worked.