Magento 1.6: addImageToMediaGallery does not add to admin store or media gallery

Magento 1.6: addImageToMediaGallery does not add to admin store or media gallery

September 3, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento 1.6: addImageToMediaGallery does not add to admin store or media gallery

Magento 1.6: Properly Adding Images to Products via Code

As a seasoned developer working extensively with e-commerce platforms like Magento, I often encounter issues related to image management during product updates or imports. In this post, we’ll delve into the challenges you’re facing and provide a comprehensive solution for properly adding images to existing products in Magento 1.6.

The Current Approach and Its Limitations

You’ve shared your current approach using addImageToMediaGallery method:

Mage::app()->setCurrentStore($target_store);
$product = Mage::getModel("catalog/product")->loadByAttribute($sku);
$product->addImageToMediaGallery(
    $local_img_path,
    array('image', 'thumbnail', 'small_image'),
    false,
    false
);
$product->save();

While this method appears to work on the frontend, it fails to add images in the admin area for the product. The issue lies in the way Magento handles multistore configurations and image attributes.

Understanding the Multistore and Image Attribute Dynamics

Magento is designed for multistore setups, which means that each store can have its own set of products with independent attributes and media galleries. When you add an image using addImageToMediaGallery, it only adds the image to the current store context ($target_store).

The three relevant entries in catalog_product_entity_varchar represent the base, small, and thumbnail images for the frontend store (store_id 4). However, these entries do not include a corresponding entry with store_id 0, which is required for the admin area.

The Manual SQL Query Workaround

You’ve discovered that manually executing four SQL queries can resolve the issue:

-- Insert or update base image for admin store
INSERT INTO catalog_product_entity_varchar (entity_type_id, attribute_id, store_id, entity_id, value)
SELECT DISTINCT 4, 79, 0, entity_id, value
FROM catalog_product_entity_varchar AS cpev
WHERE attribute_id = 79 AND store_id != 0 AND entity_id NOT IN (
    SELECT entity_id FROM catalog_product_entity_varchar WHERE store_id = 0 AND attribute_id = 79
)
ON DUPLICATE KEY UPDATE value = cpev.value;

-- Repeat for small and thumbnail images (attribute_ids 80, 81)
INSERT INTO catalog_product_entity_media_gallery (attribute_id, entity_id, value)
SELECT 82, entity_id, value FROM catalog_product_entity_varchar AS cpev
WHERE attribute_id = 79 AND store_id = 0 AND entity_id NOT IN (
    SELECT entity_id FROM catalog_product_entity_media_gallery WHERE attribute_id = 82
)
ON DUPLICATE KEY UPDATE value = cpev.value;

While this manual approach works, it is not a scalable or maintainable solution for an automated import process. It also doesn’t address the core issue of why the addImageToMediaGallery method isn’t functioning as expected.

The Proper Approach: Utilizing Magento’s Core Features

The root cause of the problem lies in the fact that the addImageToMediaGallery method only adds the image to the media gallery of the current store context. To add images to all stores, including the admin area, you need to use Magento’s built-in mass action for adding product images.

Step 1: Create a Temporary CSV File

First, create a temporary CSV file containing the SKUs or IDs of the products that require image updates:

SKU
24-MB01
24-MB02

Step 2: Use Magento’s “Import/Export” Functionality to Add Images

Navigate to System > Import/Export in your Magento admin panel. Select Product Image as the entity type and choose the temporary CSV file you created.

Step 3: Configure the Image Paths and Attributes

In the Image path (file name) field, specify the path relative to the media directory where the images are located. For example:

t/e/tempimg_791.jpg

Select the appropriate image attributes (image, thumbnail, small_image) for each row in the CSV file.

Step 4: Start the Image Import

Click on the “Run Import” button to initiate the process. Magento will automatically add the images to all relevant stores, including the admin area, ensuring that the media gallery is properly populated across the entire platform.

Conclusion: Streamlined Image Management in Magento

In summary, when dealing with image updates for products in a multistore Magento setup, it’s crucial to leverage the built-in import/export functionality rather than relying on manual SQL queries or direct method calls. This approach ensures that images are consistently added across all stores and attributes without any gaps in the media gallery.

Remember, maintaining a good knowledge of core Magento features is key to optimizing your development process. For more in-depth guides and best practices for Magento development, be sure to visit our blog at Laravel Company.

By following these steps, you will have a robust and efficient solution for managing product images across your entire Magento 1.6 installation, ensuring consistency between the frontend, admin area, and all associated stores.

Happy coding!

Magento 1 Catalog Admin Configuration

Our network