MAGENTO : Add image field in custom module

MAGENTO : Add image field in custom module

August 24, 2026 · Illustrated author avatarBy Stefan Bogdanescu
MAGENTO : Add image field in custom module

Enhancing Your Magento Extension: Adding an Image Field

In your quest to expand the functionality of your Magento extension, you’ve already taken the first step—defining an image field in your module’s admin configuration form. However, you’re rightfully wondering how to handle this image data within your controller. Let me guide you through this process with a comprehensive solution that will help you manage images seamlessly.

Defining the Image Field

You’ve correctly set up the basic structure for adding an image field:

$fieldset->addField('photo', 'image', array(
    'label'     => "Image",
    'required'  => false,
    'name'      => 'image',
));

This code snippet instructs Magento to add an image upload field named photo to your form. The name attribute must match the name used in the controller.

Handling the Image Upload

When you submit the form, the controller receives raw data as a $_POST request. To access the uploaded file, you need to include the Magento file handling library and use its methods:

First, ensure you have properly configured your module with an etc/adminhtml/system.xml file:

<config>
    <tabs>
        <custom_tab translate="label" module="your_module">
            <label>Custom Tab</label>
        </custom_tab>
    </tabs>
    <sections>
        <custom_section module="your_module">
            <label>Custom Section</label>
            <tab>custom_tab</tab>
            <sort_order>1000</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>1</show_in_store>
        </custom_section>
    </sections>
    <resources>
        <your_module_setup>
            <setup>
                <module>Magento_Backend</module>
                <version>10.0.2</version>
            </setup>
        </your_module_setup>
    </resources>
</config>

In your controller’s saveAction method, include the following code to handle the image upload:

public function saveAction()
{
    // Include the file handling library
    require_once Mage::getBaseDir('lib') . '/Varien/File/Uploader.php';
    $uploader = new Varien_File_Uploader('image');

    if ($uploader->getMediaType() != Varien_File_Uploader::MEDIA_TYPE_IMAGE) {
        return false;
    }

    // Set the upload directory and file name
    $uploader->save(Mage::getBaseDir('media') . DS . 'your_module' . DS . 'images', 'image');

    // Get the uploaded image path
    $imagePath = Mage::getBaseDir('media') . DS . 'your_module' . DS . 'images' . DS . 'image';

    // Update your model with the image path
    $model->setData('image_path', $imagePath);
    $model->save();

    return true;
}

Remember to replace 'your_module' with the actual name of your module and ensure that the media/your_module/images directory exists.

Conclusion

Incorporating images into your Magento extension is a powerful way to enhance its functionality and user experience. By following the steps outlined above, you’ve successfully added an image field to your admin configuration form and learned how to handle the uploaded file within your controller.

For more in-depth guidance on developing custom Magento extensions, I invite you to explore our comprehensive resources at Laravel Company. Our expert team offers tailored solutions for all your e-commerce needs.

Extensions Themes Admin Configuration

Our network