cancel
Showing results for 
Search instead for 
Did you mean: 

Magento file upload don't delete or update the file uploaded

Magento file upload don't delete or update the file uploaded

Just a continuation of my study about Magento Admin Grid. I'm trying to create a File Upload and I am successfully made it. However, I'm encountering a problem regarding the update and delete of the file upload form field. The other input form fields are populated with data and can be updated and deleted from the records.

 

Problem:

 

When tried to update the records, the file upload field was not displaying the name of the uploaded file, and when try to update it, the old uploaded file was not deleted but the file path was updated on the records.

 

When try to delete the records, the uploaded file was not deleted but deleted on the record.

 

I also have minor issue regarding the grid. The grid is not displaying the ID of the records and other integer or number even if they were declared on the grid.

 

Question:

 

What am I missing in my update form fields and grid?

 

Here is a sample of my form fields.

 

$fieldset->addField('title', 'text', array(
            'label' => Mage::helper('pmadmin')->__('Matrix Title'),
            'class' => 'required-entry',
            'required' => true,
            'name' => 'title',
        ));
 
        $fieldset->addField('file_path', 'file', array(
            'label' => Mage::helper('pmadmin')->__('File'),
            'value'  => '',
            'class' => 'required-entry',
            'required' => true,
            'disabled' => false,
            'readonly' => true,
            'name' => 'file_path',

            ));
 
        $fieldset->addField('short_description', 'text', array(
            'label' => Mage::helper('pmadmin')->__('Short Description'),
            'class' => 'required-entry',
            'required' => true,
            'name' => 'short_description',
        ));

 

Here is my controller

    public function editAction()
    {
        $pmadminId     = $this->getRequest()->getParam('id');
        $pmadminModel  = Mage::getModel('pmadmin/pmadmin')->load($pmadminId);
  
        if ($pmadminModel->getId() || $pmadminId == 0) {
  
            Mage::register('pmadmin_data', $pmadminModel);
  
            $this->loadLayout();
            $this->_setActiveMenu('pmadmin/items');
            
            $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Item Manager'), Mage::helper('adminhtml')->__('Item Manager'));
            $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Item News'), Mage::helper('adminhtml')->__('Item News'));
            
            $this->getLayout()->getBlock('head')->setCanLoadExtJs(true);
            
            $this->_addContent($this->getLayout()->createBlock('pmadmin/adminhtml_pmadmin_edit'))
                 ->_addLeft($this->getLayout()->createBlock('pmadmin/adminhtml_pmadmin_edit_tabs'));
                
            $this->renderLayout();
        } else {
            Mage::getSingleton('adminhtml/session')->addError(Mage::helper('pmadmin')->__('Item does not exist'));
            $this->_redirect('*/*/');
        }
    }
    
    public function newAction()
    {
        $this->_forward('edit');
    }
    
    public function saveAction() {
        $post_data=$this->getRequest()->getPost();

        if ($post_data) {
            try {                
         //save file to the destination folder   
                if (isset($_FILES)){
                    if ($_FILES['file_path']['name']) {
                        
                        $path = Mage::getBaseDir('media') . DS . 'rts' . DS .'pmadmin'.DS;
                        $uploader = new Varien_File_Uploader('file_path');
                        $uploader->setAllowedExtensions(array('PDF','pdf'));
                        $uploader->setAllowRenameFiles(false);
                        $uploader->setFilesDispersion(false);
                        $destFile = $path.$_FILES['file_path']['name'];
                        $filename = $uploader->getNewFileName($destFile);
                        $uploader->save($path, $filename);

                        $post_data['file_path']='rts/pmadmin/'.$filename;
                    }
                }
        //save file path to the database
                $model = Mage::getModel("pmadmin/pmadmin")
                ->addData($post_data)
                ->setId($this->getRequest()->getParam("id"))
                ->save();

                Mage::getSingleton("adminhtml/session")->addSuccess(Mage::helper("adminhtml")->__("File was successfully saved"));
                Mage::getSingleton("adminhtml/session")->setPmadminData(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")->setPmadminData($this->getRequest()->getPost());
                $this->_redirect("*/*/edit", array("id" => $this->getRequest()->getParam("id")));
            return;
            }

        }
        $this->_redirect("*/*/");
    }
    
    public function deleteAction()
    {
        if( $this->getRequest()->getParam('id') > 0 ) {
            try {
                $pmadminModel = Mage::getModel('pmadmin/pmadmin');
                
                $pmadminModel->setId($this->getRequest()->getParam('id'))
                    ->delete();
                    
                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Item was successfully deleted'));
                $this->_redirect('*/*/');
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
            }
        }
        $this->_redirect('*/*/');
    }

 

 

Note:

 

I am able to successfully update other form fields and the file path from the records but not the file uploaded.