Magento Module Upload Image in Admin

Magento Module Upload Image in Admin

October 4, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento Module Upload Image in Admin

How to Upload Images in Magento Admin: A Step-by-Step Guide

As you’re developing your first Magento module that includes both frontend and backend functionality, you might be wondering how to handle image uploads in the admin panel. Don’t worry - I’ll guide you through the process step by step.

Setting Up Your Module Structure

Before we dive into the image uploading logic, let’s ensure your module is structured correctly:

your_module/
|-- etc/
|   |-- module.xml
|   |-- config.xml
|
|-- etc/adminhtml/
|   |-- acl.xml
|
|-- Controller/
|   |-- Adminhtml/
|       |-- YourController.php
|
|-- Helper/
|   |-- Data.php
|
|-- Model/
|   |-- YourModel.php
|
|-- Block/
|   |-- YourBlock.php
|
|-- view/
    |-- adminhtml/
        |-- templates/

Creating the ACL Configuration

To allow your module access to the Magento backend, you need to configure the necessary ACL rules in etc/adminhtml/acl.xml:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ACL/etc/acl.xsd">
    <acl>
        <resources>
            <resource id="Magento_Backend::admin">
                <resource id="Magento_Cms::cms">
                    <resource id="Magento_Cms::pages" title="Pages">
                        <resource id="Magento_Cms::pages_index" />
                    </resource>
                </resource>
            </resource>
        </resources>
    </acl>
</config>

Creating the Image Upload Form in Your Controller

Now let’s create an adminhtml controller to handle image uploads. In Controller/Adminhtml/YourController.php:

<?php
namespace Magento\Backend\Controller\Adminhtml;

use Magento\Backend\App\Action;
use Magento\Framework\Controller\ResultFactory;

class YourController extends Action
{
    public function execute()
    {
        $resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
        return $resultPage;
    }
}

Implementing the Image Upload Logic

To handle file uploads, you can use Magento’s built-in functionality. First, create a form field for the image in your phtml template:

<form action="" method="post" enctype="multipart/form-data">
    <input type="file" name="image" accept="image/*">
    <button type="submit">Upload</button>
</form>

Next, process the uploaded file in your controller’s execute method:

public function execute()
{
    $resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);

    if ($this->getRequest()->isPost()) {
        $image = $this->getRequest()->getFiles('image');

        if (isset($image['tmp_name']) && $image['tmp_name'] != '') {
            $uploader = $this->mediaDirectory->getAdapter();
            $result = $uploader->saveFile($image);
            // Process the uploaded image
            // ...
        }
    }

    return $resultPage;
}

Conclusion

Uploading images in Magento admin is a common requirement for many custom modules. By following this guide, you should now have a clear understanding of how to structure your module and implement the necessary functionality.

Remember that correct ACL configuration is crucial to ensure your module has access to the backend features it needs. Always test your upload logic thoroughly to prevent any potential security vulnerabilities or data corruption.

If you found this guide helpful, please share it with your fellow developers, and feel free to explore more Magento development resources on LaravelCompany. Happy coding!

Extensions Themes Admin Configuration

Our network