Overwrite file names when uploading catalog images to Magento

Overwrite file names when uploading catalog images to Magento

October 3, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Overwrite file names when uploading catalog images to Magento

Overwriting Existing Files When Uploading Catalog Images in Magento

When working with product images in Magento, you may encounter a situation where you want to overwrite an existing image file with the same name as the new upload. By default, Magento appends a “_1” suffix to the filename of the second image instead of replacing the original file.

In this post, we’ll explore how to modify Magento’s core behavior to allow overwriting files when uploading catalog images.

Understanding the Current Behavior

The current behavior is controlled by the setAllowedRenameFiles() method in the Mage_Core_Model_File_Uploader class, which is used in the gallery controller for product image uploads (app/code/core/Mage/Adminhtml/controllers/Catalog/Product/GalleryController.php).

When this method is set to true, Magento will rename existing files when a new file with the same name is uploaded. This is why you see “_1”, “_2”, etc. appended to filenames after multiple uploads.

Attempted Solutions and Their Limitations

The OP mentioned trying two approaches:

  1. Setting setAllowedRenameFiles() to false. However, this doesn’t disable renaming; it just prevents the uploader from naming files differently if they exceed the maximum filename length allowed by the operating system.

  2. Commenting out the line containing setAllowedRenameFiles(). This also doesn’t solve the problem because Magento’s core still handles file naming internally.

The Correct Approach: Override the Uploader

To achieve the desired behavior of overwriting existing files, you need to override the core uploader class and modify its behavior. Here are the steps:

  1. Create a custom uploader module:

    Create a new module (e.g., Your_Company_Catalog).

  2. Override the core uploader:

    In your custom module’s etc/config.xml file, add an entry for the uploader model override:

    <global>
      <models>
        <your_module>
          <class>Your_Company_Catalog_Model</class>
        </your_module>
        <catalog_model>
          <rewrite>
            <catalog_product_entity_gallery_loader>Your_Company_Catalog_Model_Catalog_Product_Gallery_Uploader</catalog_product_entity_gallery_loader>
          </rewrite>
        </catalog_model>
      </models>
    </global>
    
  3. Create the custom uploader class:

    In your module’s Model directory, create a new file named Catalog/Product/Gallery/Uploader.php.

  4. Override the save() method:

    In this custom class, override the save() method of the core Mage_Core_Model_File_Uploader:

    public function save($destinationFolder = null)
    {
        // Your custom logic here...
    
        if ($this->getFile() && $filePath) {
            if (file_exists($filePath)) {
                unlink($filePath);  // Remove the existing file before saving the new one
            }
            rename($this->getTempName(), $filePath);
        }
    
        return $this->_getUploadedFile();
    }
    

Conclusion

To allow Magento to overwrite existing files when uploading catalog images, you need to override the core Mage_Core_Model_File_Uploader class in a custom module.

By following the steps outlined above—creating a custom uploader module and modifying its behavior through model overrides—you can achieve your desired functionality without breaking Magento’s core code or resorting to hacky solutions like changing core files directly.

Remember, it’s always best practice to use Magento’s extension system (etc/config.xml for model rewrites) rather than altering core files. This ensures the longevity and portability of your solution across Magento updates.

For a comprehensive guide on extending Magento functionality through custom modules, check out this article on Laravel Company.

Catalog Admin Configuration Errors

Our network