Magento Custom Module: multiple Image Uploader in adminhtml form

Magento Custom Module: multiple Image Uploader in adminhtml form

July 14, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento Custom Module: multiple Image Uploader in adminhtml form

Enabling Multiple Image Uploads in Magento Adminhtml Forms

Hello everyone! Today we’re going to tackle a common challenge faced by Magento developers - implementing multiple image upload functionality within an adminhtml form.

The Current Setup

The OP has already created a basic adminhtml module with four fields: name, images, URL, and email ID. They’ve successfully implemented a single image uploader using the Mage_Core_Model_File_Uploader class.

Here’s their current code snippet for the image field:

$fieldset->addField('image', 'image', array(
    'label' => Mage::helper('finder')->__('image'),
    'required' => false,
    'name' => 'image',
));

The Challenge: Multiple Image Uploads

The main issue here is that the current setup only allows for a single image to be uploaded at a time. To enable multiple images, we need to make some adjustments.

Solution: Configuring Multiple Image Uploaders

To achieve this, we’ll modify the adminhtml form and create separate fields for each additional image required. Here’s how you can do it:

Step 1: Modify the Form Definition

In your custom module’s form.phtml file, add new input fields for the multiple images:

<div class="entry-edit">
    <div class="edit-form">
        <form action="<?php echo $this->getFormActionUrl() ?>" id="item_form" method="post"
            enctype="multipart/form-data">
            <?php if ($this->getStoreId() && !is_null($this->getStore())) : ?>
                <input type="hidden" name="store_id" value="<?php echo $this->getStore()->getId() ?>">
            <?php endif; ?>

            <!-- Existing fields -->
            <!-- ... -->

            <!-- New multiple image fields -->
            <div class="field">
                <label for="additional_image1"><?php echo Mage::helper('finder')->__('Additional Image 1') ?></label>
                <input type="file" name="additional_images[]" id="additional_image1" />
            </div>

            <div class="field">
                <label for="additional_image2"><?php echo Mage::helper('finder')->__('Additional Image 2') ?></label>
                <input type="file" name="additional_images[]" id="additional_image2" />
            </div>

            <!-- ... Add more fields as needed -->

        </form>
    </div>
</div>

Step 2: Process the Uploaded Images

In your controller’s saveAction method, handle the uploaded images accordingly. Make sure to loop through the $_FILES['additional_images'] array and process each image separately.

Here’s a basic example:

public function saveAction()
{
    $data = $this->getRequest()->getPost();
    if ($data) {
        // Process regular data fields

        foreach ($_FILES['additional_images']['name'] as $key => $image_name) {
            if (($_FILES['additional_images']['error'][$key] == UPLOAD_ERR_OK)
                && (!empty($_FILES['additional_images']['tmp_name'][$key]))
            ) {
                $uploader = new Varien_File_Uploader($data);
                $uploader->setAllowedExtensions(array('jpg', 'jpeg', 'gif', 'png'));
                $uploader->addValidateCallback('callback_image_upload');
                $uploader->setAllowRenameFiles(true);

                // Save the image to the specified directory
                $result = $uploader->save(Mage::getBaseDir('media') . DS . 'finder' . DS . 'store_locator');

                // Process and save the image path to your database model
            }
        }

        // Continue with regular data saving process

    }
}

Step 3: Display Multiple Images in the Form

To display previously uploaded images, you can create an array of existing image paths and add them as hidden input fields:

<div class="field">
    <label for="existing_images"><?php echo Mage::helper('finder')->__('Existing Images') ?></label>
    <div id="existing-images-list">

        <?php foreach ($this->getExistingImages() as $image_path): ?>
            <input type="hidden" name="existing_images[]" value="<?php echo $image_path ?>" />
            <img src="<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . 'finder/store_locator/' . $image_path ?>"
                 alt="<?php echo $image_path ?>" height="120" width="120" />
        <?php endforeach; ?>

    </div>
</div>

Conclusion

Enabling multiple image uploads in a Magento adminhtml form requires careful configuration of the input fields and proper handling of uploaded images in your controller action. By following the steps outlined above, you can easily add multiple image upload functionality to your custom module.

Remember that this is a basic example, and you may need to adapt it according to your specific requirements. For more advanced solutions or if you encounter any issues, feel free to explore our resources at Laravel Company.

Happy coding!

Extensions Themes Admin Configuration

Our network