1

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();
    }
}
2

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.

  • $postData['fileinputname'] = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).'foo/' .$_FILES['fileinputname']['name']; I have tried with this code for save image path to DB – DRAJI Oct 16 '13 at 13:24
  • I have added saveAction method from controller file. please lookout my edit – DRAJI Oct 16 '13 at 13:33
  • @DRAJI. First of all this is wrong: $postData['fileinputname'] = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).'foo/' .$_FILES['fileinputname']['name']; What if there is no file uploaded? What if the file is renamed. Second: put this line Zend_Debug::dump($postData);exit; right before calling $model->save(); and add the result to your question. – Marius Oct 16 '13 at 13:38
  • But In first time, its working fine. When i have try to edit only, fileinputname saved as array – DRAJI Oct 16 '13 at 14:06
  • @DRAJI Then see how the POST looks when trying to update. That's what I've been saying in these comments. – Marius Oct 16 '13 at 14:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.