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>			            
</form>

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


if(isset($_FILES['docname']['name']) && $_FILES['docname']['name'] != '')
{
	try
	{		
		$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->setFilesDispersion(false);
		$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.

Update
———

Steps

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"/>
</reference>

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 thoughts on “File Upload in Magento

  1. 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.

    1. Yes,

      You need to add the extension list in this line

      $uploader-&gt;setAllowedExtensions(array('doc','pdf','txt','docx')); //Allowed extension for file
      
  2. shall I use this for product front end page. Because I need to get file for some of specified products

    1. could you please explain how to use it I have already used magento defaultcusom file upload option its not working properly when I am upload file am getting “Please specify product required option’s” error

    2. Is there any other field which is not displayed and compulsory, also you need to search for the error and check the code for which attribute the value is null

    3. there is no other mandatory field. without custom option its working fine. error occurs when we give custom option is mandatory

  3. 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.

    1. 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

  4. 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.

    1. Thank you so much. The issue is that I have MagentoGo. I am not sure how to go about using these steps in MagentoGo.

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

    enctype=”multipart/form-data”

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

  6. Hi,

    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.

    Thanks

    1. @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

  7. 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’] != ”)
    {
    try
    {
    $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->setFilesDispersion(false);
    $uploader->save($path,$fname); //save the file on the specified path

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

    1. 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

Your email address will not be published. Required fields are marked *

Scroll to top