I would like to add a custom button in a backend grid page, that let me upload a file in Magento.

So basically it should give me a "file selector" and then file should be processed in a controller.

Can you point out to some tutorial or any Magento default module where I can get an "inspiration" ?

3 Answers 3


Try this.. In you block-form

$fieldset->addField('file', 'file', array(
            'label'     => Mage::helper('foundation')->__('CSV'),
            'class'     => 'disable',
            'required'  => true,
            'name'      => 'file',

And in your controller

public function saveAction()
        if ($data = $this->getRequest()->getPost()) {
            if (isset($_FILES['file']['name']) && $_FILES['file']['name'] != '') {
                    $path = Mage::getBaseDir().DS.'csv'.DS;  //desitnation directory
                    $fname = $_FILES['file']['name']; //file name
                    $fullname = $path.$fname;
                    $uploader = new Varien_File_Uploader('file'); //load class
                    $uploader->setAllowedExtensions(array('CSV','csv')); //Allowed extension for file
                    $uploader->setAllowCreateFolders(true); //for creating the directory if not exists
                    $uploader->save($path, $fname); //save the
                catch (Exception $e)
                    $fileType = "Invalid file format";
        if ($fileType == "Invalid file format") {
            Mage::getSingleton('adminhtml/session')->addError(Mage::helper('foundation')->__($fname." Invalid file format"));

........... I have done my changes as per the my requirement, you can try yours,

Hope this will help you


I used this tutorial https://stackoverflow.com/questions/6734281/magento-image-upload-form-field

Not going to re-write the same steps provided in the link since I'm referencing a different section of the same site.


Checkout the class Mage_Core_Model_File_Uploader

Maybe this source of the wysiwyg editor helps you

  • You should give more details about what you mean. This is a poor answer.
    – Marius
    Commented Jul 1, 2014 at 18:58
  • 1
    The question was to "point out to some tutorial or any Magento default module where he can get an "inspiration" " - I think my answer matches exactly the expectation. Commented Jul 3, 2014 at 8:57

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.