File Upload in Magento

As you already might have noticed, there are many modules in Magento which has file uploading facility in admin end.

I had to make a custom form for file uploading in Magento in frontend, I am explaining here the way to upload a file.

Magento has a class (Varien_File_Uploader) defined for this purpose. The class file path is lib/Varien/File/Uploader.php.

Below is a sample form
Note: The file field name is ‘docname’

<form id="doc-form" name="doc-form" method="post" action="" enctype="multipart/form-data">
<label>Upload Document</label>
<input type="file" title="File" name="docname">
<button type="submit" title="Save"><span>Upload</span></button>			            

Below code for file uploading
Note: The destination directory should be writable

if(isset($_FILES['docname']['name']) && $_FILES['docname']['name'] != '')
		$path = Mage::getBaseDir().DS.'customer_documents'.DS;	//desitnation directory		
		$fname = $_FILES['docname']['name']; //file name						
		$uploader = new Varien_File_Uploader('docname'); //load class
		$uploader->setAllowedExtensions(array('doc','pdf','txt','docx')); //Allowed extension for file
		$uploader->setAllowCreateFolders(true); //for creating the directory if not exists
		$uploader->setAllowRenameFiles(false); //if true, uploaded file's name will be changed, if file with the same name already exists directory.
		$uploader->save($path,$fname); //save the file on the specified path
	catch (Exception $e)
		echo 'Error Message: '.$e->getMessage();

You can further refer this Create file/image/video upload in Backend for own module.

Hope this is helpful.



1) Create a page frm.phtml in app\design\frontend\[your package]\[your theme]\template\contacts, paste all the code as given in the post in this file

2) Create a page from Store Admin-end > CMS > Pages

3) Assign the value ‘fupload’ in URL Key

4) Browse to Pages > Design (Tab in the left column)

5) Paste the content

<reference name="content">
<block type="core/template" name="newForm"  template="contacts/frm.phtml"/>

6) The form action URL will change as per Url key of the admin page, for example we have used ‘fupload’, so the action URL will be

<form id="doc-form" name="doc-form" method="post" action="<?php echo Mage::getBaseUrl().'fupload'; ?>" enctype="multipart/form-data"> 

The page can be seen at http://storeurl/fupload

30 Responses to “File Upload in Magento”

  1. Magecoder says:

    Thanks buddy. worked perfect and saved my hour. 😉

  2. Mary Johnson says:

    Hi, Thanks for this wonderful blog post.The question i want to ask Is that,Can this coding support all types of files uploads like text file, media file,Mp3 ,PDF Etc.If it can then I need your assistance to apply it on my web store.

  3. midun says:

    shall I use this for product front end page. Because I need to get file for some of specified products

  4. andres says:

    Hi, nice blog. I got an issue with this code, I need to upload file in magento admin in order comments, I dont’ get it when you put this :$_FILES[‘docname’][‘name’], the my input is the next: , how do I have to set this in your code. Thanks for your help.

    • Pragnesh says:

      Please check whether you have forgotten to add enctype=”multipart/form-data” in the form tag, this is required if the form is used for uploading files

  5. AM says:

    Hello DWRoot,
    I am a novice in Magento. Can you provide step by step instructions on how and where to add the above mentioned code for file uploading? I’ll compensate you for your service.
    Thanks a lot.

  6. kads says:

    How can i upload multiple images in array using same above uploader class?

  7. Rohan says:

    I refereed this code but not works for me
    I tested in various ways and finally I found that
    I missed the


    section into my form tag and then It works
    Thanks for this

  8. farah says:

    i want to resize and privew the upload file image how can i do that

  9. Vishal says:


    I am new to magento. I also require same kind of funtionality to allow the people to upload their CV on frontend. For this I have created a page ‘carrier’ from admin end. Can you please explain in detail where this code will go which you have mentioned above.


    • Pragnesh says:

      @Vishal – You can include a block in the content of the ‘carrier’ page in admin end, this block need to be created and the post action should be in any existing or new controller. I suggest you to create a new module with block and controller for your requirement

  10. Michael says:

    I hope you are there …

    Where do I put this bit of code? I am having some sort of brain drain. I can’t seem to understand where this goes. Thanks.

    if(isset($_FILES[‘docname’][‘name’]) && $_FILES[‘docname’][‘name’] != ”)
    $path = Mage::getBaseDir().DS.’customer_documents’.DS; //desitnation directory
    $fname = $_FILES[‘docname’][‘name’]; //file name
    $uploader = new Varien_File_Uploader(‘docname’); //load class
    $uploader->setAllowedExtensions(array(‘doc’,’pdf’,’txt’,’docx’)); //Allowed extension for file
    $uploader->setAllowCreateFolders(true); //for creating the directory if not exists
    $uploader->setAllowRenameFiles(false); //if true, uploaded file’s name will be changed, if file with the same name already exists directory.
    $uploader->save($path,$fname); //save the file on the specified path

    catch (Exception $e)
    echo ‘Error Message: ‘.$e->getMessage();

    • Pragnesh says:

      This can go in the save/upload action, you can either create a new function or can write in already existing function in your controller file, which handles the data after form submit

Leave a Reply