Home » Magento19 February 2010

Magento: How to upload file?

In adminhtml, you might have the following code in any form. Here ‘logo‘ is the name of the input type file. Or, you may have any HTML Form with the File field in the frontend page. Remember that your form’s enctype should be multipart/form-data.

You can easily upload file using Varien_File_Uploader class. The class file path is lib/Varien/File/Uploader.php.

– In the code below, the file field name is ‘logo‘.
– You can add or remove allowed extension for upload through setAllowedExtensions function.
– If setAllowRenameFiles function’s parameter is set true, then the uploaded file name will be changed if some file with the same name already exists in the destination directory.
– The file is uploaded in media directory. You can add your own directory there. But remember that your directory should be writable.
– save function is used to save the file with a specific name and to a specific path.

Hope this helps. Thanks.


Get New Post by Email

Find me on

FacebookTwitterGoogle+LinkedInRSS Feed
  • Milind

    Hi mukesh it is nice tutorial thanks
    but now i have one problem i have upload .doc,.txt file and i uploaded into media.uploaded_file folder now i have show file’s data into text area how it possible pls give me guideline

  • open the file with fopen, read the content, and then display it in textarea. I think, this way it should work.

  • David


    I am not finding an array of allowedExtensions in version 1.4x /lib/Varien/File/Uploader.php

    Did it move?

    In a js file?

    Please help… I am attempting to make the product image upload accept pdf docs + jpg, gif, png, etc.

    Respond to DavidCate@live.com

  • Hardik

    Hi Mukesh,
    Nice Article!!
    Thats what I looking for.
    But I have question for you if you have time.

    I am able to upload logo file now and it is working fine.
    But how can I save in Magento so I don’t have to change Magento admin->System->Configuration->Design-> logo Image Src.


  • Jai

    Hi Mukesh,

    I want to add image upload functionality on review form. Can you please help me.


  • Hi, Mukesh

    Your code works fine for image uploading but when i edit the form without upload any image it throws error:

    Warning: preg_match() expects parameter 2 to be string, array given in /homepages/17/d321292393/htdocs/earthology/lib/Varien/Data/Form/Element/Image.php on line 61

    please help if you have any solution for this.

  • Svens0n82

    Is this Uploader working in frontend too? I (am trying) to extend the registration form that the user can upload 1 file. Upload throws no error but there also is no file in the media directory after upload.

  • BenRilliger

    Hi Mukesh,
    thanks for that great post.
    How can I upload mutliple images? I want to add an image gallery to my magento shop and do not want to add an extra field for every image.
    Can you help me?


  • Hi Mukesh Chapagain,
    Thanks for your post.

    But I can not find this file to add my code,

    Please, tell me exactly this file, where is it in adminhtml ?

    Thank you and waiting you……

  • Morris

    Hey Mukesh

    thks for your wonderful articles!

    Do you have any suggestions on how to upload images in the products area? magento 1.5 show 100% upload but the images they are not showing. Maybe you did answer this already – where can i find it?

    Please help.



  • Jpomes2

    Thank you very much! just what we looking

  • Luke

    Not trimming the $_FILES[$key][‘name’] input?

  • Mani

    How can I upload an image from a custom front-end form and save it to a custom database table.

    PLease Help!!