Image display problem after save in custom module

If you Save the Form for the second time without image. An empty array value will get inserted in the database, Which makes the previous image preview below image

To resolve this The save function of the Adminhtml Controller of the custom module must be updated as shown in the below code.

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

	if(isset($_FILES['filename']['name']) && $_FILES['filename']['name'] != '') {
		try {
		     /* File Upload code here */
		     $uploader = new Varien_File_Uploader('filename');
		     // remaining lines
		 } catch (Exception $e) {


             //this way the name is saved in DB
   	$data['filename'] = $_FILES['filename']['name'];
//--- Else Condition must be added to Prevent Image Preview Disappear ---
      else {
        unset($data['filename']);  // Unset filename part when image upload field is empty



Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s