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()) 

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


  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',


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


Grid page:

enter image description here

Edit page:

enter image description here

  • you are adding the image or other file? Nov 26, 2015 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, 2015 at 7:08
  • added the answer it is related to save function. Nov 26, 2015 at 7:16

2 Answers 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? Aug 30, 2018 at 13:22
  • @SunnyRahevar controller Sep 3, 2018 at 4:46

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

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

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

    $model = Mage::getModel('pmadmin/uploadfile');      
    try {
    } catch (Exception $e) {
  • 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, 2015 at 0:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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