Magento – Magento – get uploaded file name in form upload field

file uploadjavascriptmagento-1.9PHP

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

Best Answer

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'];
}
Related Topic