Magento – To save image without overriding

imagemagento-1.7

I have created custom module with image upload attribute.Its working Good. But when i edit that row in a grid, After entered save button, image path saved as Array. So i cant get an image in grid and also in frontend.I have paste my code here

    if($_FILES['fileinputname']['name'] != '') {

try {    
     $uploader = new Varien_File_Uploader('fileinputname');
     $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
     $uploader->setAllowRenameFiles(true);
     $uploader->setFilesDispersion(false);

     $path = Mage::getBaseDir('media').'/foo/';
    // move_uploaded_file( $_FILES['userFile']['tmp_name'], $path);
     $uploader->save($path, $_FILES['fileinputname']['name']);
     }
     catch (Exception $e) {
     echo 'Error Message: '.$e->getMessage();
}

I cant find what changes will i make here. Help me guys

EDIT

 public function saveAction()
{
    Zend_Debug::dump();
    if ($postData = $this->getRequest()->getPost()) {

        /// ---START code for save image attribute----
        if($_FILES['fileinputname']['name'] != '') {

try {    
     $uploader = new Varien_File_Uploader('fileinputname');
     $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
     $uploader->setAllowRenameFiles(true);
     $uploader->setFilesDispersion(false);

     $path = Mage::getBaseDir('media').'/foo/';
    // move_uploaded_file( $_FILES['userFile']['tmp_name'], $path);
     $uploader->save($path, $_FILES['fileinputname']['name']);
     }
     catch (Exception $e) {
     echo 'Error Message: '.$e->getMessage();
}
}


$postData['fileinputname'] = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).'foo/' .$_FILES['fileinputname']['name'];


//$data['popup_image'] = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . 'p_gallery/'. $_FILES['popup_image']['name'];

}
// --END---
        $model = Mage::getSingleton('foo_bar/baz');
        $model->setData($postData);

        try {
            $model->save();

            Mage::getSingleton('adminhtml/session')->addSuccess($this->__('The baz has been saved.'));
            $this->_redirect('*/*/');

            return;
        }  
        catch (Mage_Core_Exception $e) {
            Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
        }
        catch (Exception $e) {
            Mage::getSingleton('adminhtml/session')->addError($this->__('An error occurred while saving this baz.'));
        }

        Mage::getSingleton('adminhtml/session')->setBazData($postData);
        $this->_redirectReferer();
    }
}

Best Answer

Most probably, when the file is not uploaded the field fielinputname comes as an array through post. Try this:

$data = $this->getRequest()->getPost();
if($_FILES['fileinputname']['name'] != '') {
    //your upload code here
}
else {
   if (isset($data['fileinputname']['value'])){
      $data['fileinputname'] = $data['fileinputname']['value'];
   }
}

If that doesn't work, post the saveAction method from your controller in the question, and the way the $_POST looks in the same action.

Related Topic