5

I successfully create a module that upload file to the directory and save the path to the database.

My problem is that, when I tried to update the uploaded file, the form upload field and button don't display the name of the uploaded file. To make it short, the upload field value was empty, supposed to be it was populated coming from the database. Also if I tried to select another file and save it, the path was save on the database and the file was uploaded in the directory, however the problem was the old file was not deleted or updated.

I tried to view class Varien_Data_Form_Element_File and saw that it has

public function __construct($attributes=array()) 
{
    parent::__construct($attributes);
    $this->setType('file');
    $this->setExtType('file');
}

only this function. Unlike in the class Varien_Data_Form_Element_Image that has different functions that adds select box and input hidden fields.

Question:

  1. How do I update the uploaded file?
  2. How do I Delete the uploaded file?

This is 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',
));

Note:

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

UPDATE:

Grid page:

enter image description here

Edit page:

enter image description here

  • you are adding the image or other file? – Qaisar Satti Nov 26 '15 at 7:06
  • Yes, the other fields are added to the records. When try to update the record, these fields has the value coming from the database but the upload form field don't contain the file name or path from the database. – rodge Nov 26 '15 at 7:08
  • added the answer it is related to save function. – Qaisar Satti Nov 26 '15 at 7:16
2

You have to add else statment if the user is not uploading for the edit. $postdata['file_path']['value'] this is created by Magento automatically.

if(isset($_FILES['file_path']['name']) && (file_exists($_FILES['file_path']['tmp_name']))) {
    // something ....
} else {
    $postdata['file_path'] = $postdata['file_path']['value'];
}
  • Where i have to put this code? In the controller or in the form.php file? – Sunny Rahevar Aug 30 '18 at 13:22
  • @SunnyRahevar controller – Qaisar Satti Sep 3 '18 at 4:46
0

Add bellow code in your upload action in controller file

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

    if(isset($_FILES['file_path']['name']) && $_FILES['file_path']['name'] != '') {
        try {   
            $uploader = new Varien_File_Uploader('file_path');
            // Any extention would work
            //$uploader->setAllowedExtensions(array('csv','jpeg','gif','png'));
            $uploader->setAllowRenameFiles(false);
            $uploader->setFilesDispersion(false);

            $path = Mage::getBaseDir('var') . DS ;
            $uploader->save($path, $_FILES['file_path']['name'] );
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());        
            }

        //this way the name is saved in DB
        $data['file_path'] = $_FILES['file_path']['name'];
    }


    $model = Mage::getModel('pmadmin/uploadfile');      
    $model->setData($data)
        ->setId($this->getRequest()->getParam('id'));
    try {
        $model->save();
    } catch (Exception $e) {
        Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
    }
}
  • Hello @Abdul, I have no problem with saving the file path to the DB. My problem is during the update. On the Edit page, all the forms has values coming from the DB, they can be updated and saved, however, the Upload Field is empty on the Edit page. If I tried to browse file and save it, the records is updated and the file was uploaded in the directory but the old file was still there. – rodge Nov 27 '15 at 0:35

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.