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
Thanks buddy. worked perfect and saved my hour. 😉
Glad to hear that
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.
Yes,
You need to add the extension list in this line
shall I use this for product front end page. Because I need to get file for some of specified products
You can create an Product attribute for file upload, thats the proper way
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
Can you share the link to refer the error
http://drugbazar.in/index.php/pellentesque-porttitor-22.html
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
there is no other mandatory field. without custom option its working fine. error occurs when we give custom option is mandatory
Have you checked the error from where it is generated and back-traced it ?
no.. still the error occurs
I mean to say, you need to debug the code from where the error is generating
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.
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
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.
I have updated the blog post with details steps, hope this is helpful
Thank you so much. The issue is that I have MagentoGo. I am not sure how to go about using these steps in MagentoGo.
Sorry, I have not used MagentoGo yet
How can i upload multiple images in array using same above uploader class?
You need to make a loop for the files array
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
i want to resize and privew the upload file image how can i do that
@farah – There are PHP core functions available for resizing, please check that out
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
@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
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();
}
}
——–
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