Blog

How to Add File Upload Fields to Magento 2 Forms: A Comprehensive Guide

TL;DR

The Magento contact form has no file field. To add file uploads to a Magento 2 form with WebForms Suite (WebForms 4): go to Content → WebForms → Manage Forms, click the form’s name and click Form Builder. Drag a File field from the Files & Media group onto the form. On its Validation tab, set Allowed File Extensions, Allowed File Size and Maximum Files in Dropzone, then click Publish Changes. Visitors drop files onto the form or pick them from their device. The files are kept outside the public folder, and a notification can attach them. WebForms Suite is a paid extension for Magento 2.4.6–2.4.9 and PHP 8.1–8.5, on Luma, Hyvä and Breeze.

File upload fields let customers attach documents and photos to a Magento 2 form: a receipt, a floor plan, photos of a damaged item. This guide shows two ways to add them: your own module built on Magento’s file uploader, or the File and Image fields of WebForms Suite.

Introduction

The Contact Us form that comes with Magento has four fields: name, email, phone number and a comment. It has no file field, so a store that needs attachments needs its own code or a form extension. This guide covers both. The first part is a minimal module for developers, built on Magento\Framework\File\UploaderFactory. The second part uses WebForms Suite, a form builder in the Magento admin: you drag a File field onto a form, set the allowed file types, the largest size and the number of files, and publish. No code is needed.

WebForms Suite is a paid extension: €199 for Magento Open Source and €499 for Adobe Commerce, a one-time payment with 12 months of updates and support. There is no free version and no trial. You can try the live demo before you buy.

WebForms 4 works on Magento 2.4.6 to 2.4.9 with PHP 8.1 to 8.5, and requires the Magento Page Builder and Login as Customer Assistance modules. Luma needs nothing extra. Hyvä and Breeze stores install a separate WebForms package for their theme, included in the license. The largest file is set on each field. When a field has no limit, the store’s File Upload Limit (KB) applies, 5000 KB unless you change it. The server that runs your store has its own upload limits, and they must be higher than the limit you set in WebForms.

Ready to add file uploads?
See WebForms 4 on the live demo, then add WebForms Suite to your store.

Build It Yourself: The Free DIY Approach

If you have a Magento developer on hand and the appetite to maintain the code yourself, you can wire up file uploads using only platform classes — no third-party module required. Below is a working minimal example: one <input type="file"> field, server-side validation, an admin email notification. The full source is published on GitHub under the MIT license.

What you'll build

  • A single-file upload field on a custom storefront form
  • Server-side type and size validation via Magento\Framework\File\Uploader
  • Files saved to pub/media/custom-uploads/, spread over subfolders by dispersion
  • An admin email notification with the storage path

The minimum viable code

app/code/MageMe/FileUploadDemo/registration.php

<?php
declare(strict_types=1);

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'MageMe_FileUploadDemo',
    __DIR__
);

etc/frontend/routes.xml

<router id="standard">
    <route id="fileupload" frontName="fileupload">
        <module name="MageMe_FileUploadDemo"/>
    </route>
</router>

view/frontend/templates/form.phtml

<form action="<?= $escaper->escapeUrl($block->getUrl('fileupload/form/post')) ?>"
      method="post" enctype="multipart/form-data">
    <?= $block->getBlockHtml('formkey') ?>
    <input type="file" name="upload" required
           accept=".pdf,.jpg,.jpeg,.png,.docx"/>
    <button type="submit">Upload</button>
</form>

The getBlockHtml('formkey') call injects Magento's CSRF token. The controller implements HttpPostActionInterface, so Magento’s built-in form-key check applies to this non-AJAX POST action and refuses a request without a valid key. For broader storefront form security (CAPTCHA and bot checks), there's more to do.

Controller/Form/Post.php (excerpt)

$uploader = $this->uploaderFactory->create(['fileId' => 'upload']);
$uploader->setAllowedExtensions(['pdf','jpg','jpeg','png','docx']);
$uploader->setAllowRenameFiles(true);
$uploader->setFilesDispersion(true);

$mediaDir = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA);
$result = $uploader->save($mediaDir->getAbsolutePath('custom-uploads'));

The list of allowed extensions is one control among several. Keep it narrow, and also enforce a size limit, check the MIME type and the file content, save the file under a name your server chooses, and store uploads outside the public web root (see the note on pub/media below). Read the OWASP File Upload Cheat Sheet before you ship. setFilesDispersion(true) puts each file in a subfolder named after the first two characters of its name, such as custom-uploads/r/e/receipt.pdf, so one folder does not fill up with thousands of files. setAllowRenameFiles(true) adds _1, _2 and so on to a name that already exists, so a new upload never overwrites an older one.

pub/media is served by the web server: anyone who knows a file’s address can open it. For private documents, save them outside the public web root, for example under DirectoryList::VAR_DIR, and let your team download them through an admin controller that checks permissions.

Model/Mailer.php (excerpt)

$this->transportBuilder
    ->setTemplateIdentifier('mageme_fileupload_admin_notification')
    ->setTemplateOptions(['area' => 'frontend', 'store' => $storeId])
    ->setTemplateVars(['file_name' => $fileName, 'file_path' => $filePath])
    ->setFromByScope('general')
    ->addTo($adminEmail)
    ->getTransport()
    ->sendMessage();

The notification carries the storage path, not the file as an attachment. Magento's email layer doesn't accept attachments through the standard TransportBuilder — that's a separate piece of work (see scope below). For deeper customization of the notification template itself, see our guide on customizing Magento 2 email templates with PHTML.

Full module source, install instructions, and unit tests: github.com/mageme/magento2-file-upload-example

What you'll still need to build

A working upload field is just the start. For a production-grade implementation you'll also need:

  • An admin UI to list, preview, and download uploaded files
  • Multi-file upload with drag & drop, instead of one file at a time
  • Email file attachments on admin notifications, not just download links

None of these come out of the box with Magento. Plan for the development work and for maintenance through Magento upgrades.

When DIY makes sense

The DIY route is the right call when you have one simple form, an in-house Magento developer, and bandwidth for ongoing maintenance. If uploads are part of a customer-facing workflow, compare that maintenance with the price of an extension. The rest of this guide covers the extension route: first the upload fields, then the step-by-step setup in WebForms Suite, and a comparison of the options at the end.

Understanding File Upload Fields in Magento 2

WebForms Suite has two upload fields, both in the Files & Media group of the form builder. The Product Options add-on, included in the license, brings uploads to the product page:

File field

Documents and photos. You choose the allowed file types, the largest size and how many files a visitor can add.

Image field

Pictures only: jpg, jpeg, gif and png. It can make large photos smaller when they arrive.

Product page upload

With Product Options, a form becomes the options of a product. Its files are saved when the customer clicks Add to Cart.

Both fields show a drop area that says “Add files or drop here”, with the allowed file types listed under it. Each file is uploaded as soon as the visitor chooses it and gets a card with its name and size. A picture shows a small preview. A file that breaks a rule stays in the list with the reason in red, so the visitor knows what to fix.

Step-by-Step Guide: Adding File Uploads with a Dropzone to Magento 2 Forms

The steps below use the WebForms 4 form builder and work on any form. The File upload request recipe in the documentation builds a complete upload form from a blank one, and the File field page lists every setting.

  1. Open the form builder
    • Go to Content → WebForms → Manage Forms and click the form’s name.
    • Click Form Builder.
    • For a new form, click Add Form, click Blank form, then Start from Scratch.
  2. Add a File field
    • In the left panel, find the Files & Media group, or type File in Search elements....
    • Drag File onto the form.
    • In Label, enter what you ask for, for example “Proof of purchase”.
    • Turn on Required if every visitor must add at least one file.
  3. Set the file rules
    • Click the Validation tab.
    • In Allowed File Extensions, enter the types you accept, one per line, such as pdf and jpg. Visitors see them under the drop area, and a file of another type gets a red message, as in the example below.
    • In Allowed File Size, enter the largest file in kilobytes: 1000 KB is about 1 MB. When it’s empty, the store’s File Upload Limit (KB) applies, 5000 KB unless you change it.
    • In Maximum Files in Dropzone, enter how many files a visitor can add. When it’s empty, the limit is 5.

    Some file types are always refused, even when you list them, because a web server or a browser could run them: web pages such as html, server scripts such as php, and programs such as exe or js. The visitor sees the reason under the file.

    WebForms 4 File field on the storefront: receipt.pdf accepted with its size, receipt-photo.gif marked in red because gif is not in the allowed types pdf, jpg, jpeg, png, heic and webp listed in the drop area
  4. Check the drop area
    • Click the General tab.
    • Enable Dropzone is on for a new field and shows the drop area. With it off, the field shows a plain Choose File button.
    • In Dropzone Text, replace “Add files or drop here” with your own words if you want.
  5. Use an Image field for pictures
    • When you only want pictures, drag Image onto the form instead of File (see Image field). It always takes jpg, jpeg, gif and png.
    • Turn on Resize Uploaded Image and set Maximum Width and Maximum Height in pixels. The picture keeps its proportions: with both set to 800, a photo of 2400 × 1800 pixels is saved at 800 × 600. Only the smaller picture is kept.
    • For phone photos in heic or webp, use a File field and add those types to Allowed File Extensions.
  6. Show the field only when needed (optional)

    A rule can show the upload field only when it applies, for example a proof of purchase only when the item arrived damaged. The rule starts on a field with options, such as a list of return reasons. A File field can’t start a rule itself.

    • Click Logic in the top bar of the builder, then New rule.
    • In Trigger field, select the field with options. In Condition, select is, and under Values select the options that count.
    • In Action, select Show. Under Fields & sections, click the arrow next to the File field’s section and select the field.
    • Click Save rule.
    WebForms 4 New rule panel: Show Describe the damage when Reason is Arrived damaged, with the trigger field, condition, value, action and target set. A File field is picked as the target the same way
  7. Publish and test
    • Click Publish Changes in the builder’s top bar, then Publish in the dialog. Visitors see your changes only after you publish.
    • For a new form, click Next: Settings, turn on Enable Form, set a URL Key under Placement and click Save.
    • Open the form on your store. Add an allowed file and one of a type you didn’t allow, and check that the second one shows a red message.
    • Remove the refused file with its ×, send the form and find the submission in Content → WebForms → Inbox.

If a file is refused or the form won’t send, File upload fails lists the checks in order, from the file type to your server’s own upload limits.

Which option fits your store?

Three ways to collect files on a Magento 2 store.

OptionFile uploadCost
Magento Contact Us formNone. Four fields: name, email, phone number and a comment.Part of Magento
Your own module (code above)What you build: one field per form in the example. A drop area, an admin list and email attachments are extra work.Developer time and upkeep through Magento upgrades
WebForms SuiteFile and Image fields on any form, with a drop area, type, size and number limits, downloads in the Inbox and email attachments.€199 Community, €499 Enterprise, one-time, 12 months of updates and support

Managing Uploaded Files in the Magento Admin

Files that visitors send are kept with their submission. WebForms stores them on your server’s own disk, by default in a WebForms folder outside the public web folder. A file link opened from outside the admin leads to your store’s page-not-found page unless you allow customer downloads for the form. See Uploaded files in the documentation.

Files in the Inbox

Open the submission from Content → WebForms → Inbox and click a file’s name to download it. Pictures show a preview.

Download Files

One click at the top of the Submission tab downloads every file of the submission as one ZIP file.

Access by admin role

An administrator whose role doesn’t include the form can’t open its submissions, and a file link is refused. Roles are set in System → Permissions → User Roles.

Customer downloads

Off by default. Allow Frontend File Downloads in the form’s Access section lets customers open their files in their account. The link then works for anyone who has it.

Scheduled cleanup

In the form’s Data Retention section, Enable Periodic Files Cleanup deletes the files of submissions older than Files Cleanup Period (Days) and keeps the submissions. It needs Magento cron, and deleted files can’t be recovered.

Enabling File Attachments in Email Notifications

In WebForms 4, the emails of a form are its notifications. Each notification can carry the uploaded files. WebForms sends email only while Deliver Notifications is on.

  1. Go to Content → WebForms → Manage Forms and click the form’s name.
  2. Click Notifications, then the notification’s name. To add an email to your team, click the arrow next to Add New Notification and select Admin Notification under Presets, then click the new card.
    WebForms 4 Presets menu under the arrow next to Add New Notification: Admin Notification, Customer Confirmation, Approved, Not Approved, Status Update, Assignment Notification and Blank Notification
  3. In the Attachments block, turn on Attach Uploaded Files. For a new notification, also turn on Status. Click Save.
    Attachments block of a WebForms 4 notification: Attach PDF set to Staff Document, which attaches the Office Copy, If PDF Generation Fails set to Send Email Without PDF, and Attach Uploaded Files switched on

An email to your team’s fixed addresses gets every uploaded file, except the files of a field with Hide in Submissions on. An email to an address from the submission, such as the visitor’s, gets only the files of fields the visitor can see. The files of one email can’t be larger together than Attachment Size Limit (MB), 10 MB unless you change it. A file that doesn’t fit isn’t attached, and the email names it instead. Without Attach Uploaded Files, the email lists each file by name and size, and the files stay in the admin. The same block can attach a PDF of the submission; see Attachments and PDFs in emails.

Which option should you pick?

Write your own module if you have one simple form, a Magento developer and time to maintain the code. The example above is a working start. Plan storage outside the public media folder, an admin view of the files and email attachments on top of it.

Choose WebForms Suite if visitors send files on several forms, or if your team needs the files in the admin and in their email without custom code. The File and Image fields, the Inbox and the notification attachments are part of the license. The Product Options add-on, also included, puts file uploads on product pages.

The Magento Contact Us form is not an option for files: it has no upload field. For signup forms, see custom registration form fields.

WebForms Suite: €199 for Magento Open Source, €499 for Adobe Commerce. One-time payment, 12 months of updates and support. Try the live demo.

Get WebForms Suite

Key Takeaways

  • No upload in the Magento contact form — it has four fields and no file field.
  • Custom code — UploaderFactory with a narrow list of allowed extensions; pub/media is public, so keep private files elsewhere.
  • WebForms Suite — drag a File field onto the form, set Allowed File Extensions, Allowed File Size and Maximum Files in Dropzone, then publish.
  • Image field — pictures only, with optional resizing of large photos.
  • Private by default — files are kept outside the public folder and downloaded from the submission in the Inbox.
  • Email attachments — Attach Uploaded Files sends the files with a notification.

FAQs: Common Questions About File Uploads in Magento 2 Forms

help_outline What file types can be uploaded using MageMe WebForms? expand_more

A File field takes the types you list in its Allowed File Extensions setting, one per line, such as pdf, jpg or heic. An Image field takes only jpg, jpeg, gif and png. Some types are always refused, even when you list them, because a web server or a browser could run them: web pages such as html, server scripts such as php, and programs and scripts such as exe, js or sh. A file whose content is a PHP, Python or Perl script is refused too, whatever its name.

help_outline Is there a limit to the file size that can be uploaded? expand_more

Yes. Each field has an Allowed File Size in kilobytes (1000 KB is about 1 MB). When it is empty, the store limit applies: File Upload Limit (KB), 5000 KB by default, for a File field, and Image Upload Limit (KB), 1000 KB by default, for an Image field. Your server also has its own upload limits. They must be higher than the limit you set in WebForms, or the file never reaches WebForms.

help_outline Can customers upload multiple files at once? expand_more

Yes. A File or Image field with Enable Dropzone on, which is the default, takes several files. Maximum Files in Dropzone sets how many; when it is empty, the limit is 5. When the limit is reached, the drop area disappears.

help_outline How are uploaded files stored and managed? expand_more

Files are kept with the submission on your server’s own disk, by default in a WebForms folder outside the public web folder. Your team opens the submission from Content → WebForms → Inbox and clicks a file name to download it, or clicks Download Files to get all files of the submission as one ZIP. An administrator whose role does not include the form cannot open its files. Customers can open their own files only if you turn on Allow Frontend File Downloads for the form.

help_outline Is WebForms file upload compatible with Hyvä themes? expand_more

Yes. Hyvä stores install the WebForms Hyvä package, which needs Hyvä theme module 1.3.11 or later. Breeze stores install the WebForms Breeze package, which needs Breeze 2.0 or later. Both packages are included in the license, and Luma needs nothing extra. The File and Image fields work on all three.

help_outline Can a file upload field be conditional? expand_more

Yes. A rule on the form’s Logic board can show or hide a File or Image field. The rule starts on a field with options, for example: show Proof of purchase only when the reason is Arrived damaged. A File or Image field cannot start a rule itself.

help_outline What happens to uploaded files when a form entry is deleted? expand_more

Deleting a submission in the Inbox deletes it together with its uploaded files. WebForms has no trash and no undo. Copies outside WebForms stay: emails already sent with the files attached, files someone downloaded, and server backups. To delete only old files and keep the submissions, turn on Enable Periodic Files Cleanup in the form’s Data Retention section. To erase everything one person sent, use Erase Personal Data.

help_outline Can customers upload a file on the Magento 2 product page? expand_more

Yes, with the Product Options add-on, which is included in the WebForms Suite license. It turns a form into the options of a product, so a File field in that form shows on the product page. When the customer clicks Add to Cart, the form’s submission is saved with the files. A file uploaded on the product page that never goes into the cart is removed within two hours.

help_outline Does the default Magento 2 contact form support file uploads? expand_more

No. The Contact Us form of Magento Open Source and Adobe Commerce has four fields: name, email, phone number and a comment. To accept files, you need your own code, like the example in this guide, or a form extension such as WebForms Suite.