How to fix the issue: Product images missing in backend but not in frontend

June 22, 2012  |  54 Comments  |  by Raj (MagePsycho)  |  Latest, Magento


Recently i have to fix a strange issue for one of my client. Issue was that product images were missing from ‘Images’ tab of product edit form (as depicted below), though they were displaying fine in the frontend.

Missing Product Images In Backend

After going through the table relationship for catalog product images (as shown below), it was found that required rows were missing in table ‘catalog_product_entity_media_gallery’. Note that table ‘catalog_product_entity_media_gallery_value’ is not necessary unless you want to sort or label the product images.

Schema For Product Images

By inspecting the html code of Images tab of product edit form via firebug, it was seen that ‘value’ attribute had empty json:

<input type="hidden" value="[]" name="product[media_gallery][images]" id="media_gallery_content_save">

Further Debugging:
Using template path hints extension for admin: Easy Template Path Hints, it was found that the responsible template and block class were: ‘app/design/adminhtml/default/default/template/catalog/product/helper/gallery.phtml’ and ‘Mage_Adminhtml_Block_Catalog_Product_Helper_Form_Gallery_Content’ respectively.

Looking at the block class and .phtml code it was found that Input element has empty json because of Mage_Adminhtml_Block_Catalog_Product_Helper_Form_Gallery_Content::getImagesJson() method.

When populated the images from table ‘catalog_product_entity_varchar’ to ‘catalog_product_entity_media_gallery’ for those products, products images were seen at the backend(refer to the code below for more info).
Here goes the steps for fixing:


1> Prepare CSV file(update_missing_images.csv) with only one field: sku and upload to the root of Magento installation.
Note: This will contain the sku of those products whose images are missing at the backend.

2> Create a file: update_missing_images.php and upload to the root of magento installation and paste the following code:

 * @author		MagePsycho <>
 * @website
 * @category	Export / Import
$mageFilename = 'app/Mage.php';
require_once $mageFilename;
ini_set('display_errors', 1);
Mage::register('isSecureArea', 1);


/***************** UTILITY FUNCTIONS ********************/
function _log($message, $file = 'update_missing_images.log'){
	Mage::log($message, null, $file);

function _getIndex($field) {
	global $fields;
	$result = array_search($field, $fields);
	if($result === false){
		$result = -1;
	return $result;

function _getConnection($type = 'core_read'){
	return Mage::getSingleton('core/resource')->getConnection($type);

function _getTableName($tableName){
	return Mage::getSingleton('core/resource')->getTableName($tableName);

function _getAttributeId($attribute_code = 'price'){
	$connection = _getConnection('core_read');
	$sql = "SELECT attribute_id
				FROM " . _getTableName('eav_attribute') . "
				entity_type_id = ?
				AND attribute_code = ?";
	$entity_type_id = _getEntityTypeId();
	return $connection->fetchOne($sql, array($entity_type_id, $attribute_code));

function _getEntityTypeId($entity_type_code = 'catalog_product'){
	$connection = _getConnection('core_read');
	$sql		= "SELECT entity_type_id FROM " . _getTableName('eav_entity_type') . " WHERE entity_type_code = ?";
	return $connection->fetchOne($sql, array($entity_type_code));

function _getIdFromSku($sku){
	$connection = _getConnection('core_read');
	$sql		= "SELECT entity_id FROM " . _getTableName('catalog_product_entity') . " WHERE sku = ?";
	return $connection->fetchOne($sql, array($sku));

function _checkIfSkuExists($sku){
	$connection = _getConnection('core_read');
	$sql		= "SELECT COUNT(*) AS count_no FROM " . _getTableName('catalog_product_entity') . "	WHERE sku = ?";
	$count		= $connection->fetchOne($sql, array($sku));
	if($count > 0){
		return true;
		return false;

function _checkIfRowExists($productId, $attributeId, $value){
	$tableName  = _getTableName('catalog_product_entity_media_gallery');
	$connection = _getConnection('core_read');
	$sql		= "SELECT COUNT(*) AS count_no FROM " . _getTableName($tableName) . " WHERE entity_id = ? AND attribute_id = ?  AND value = ?";
	$count		= $connection->fetchOne($sql, array($productId, $attributeId, $value));
	if($count > 0){
		return true;
		return false;

function _insertRow($productId, $attributeId, $value){
	$connection				= _getConnection('core_write');
	$tableName				= _getTableName('catalog_product_entity_media_gallery');

	$sql = "INSERT INTO " . $tableName . " (attribute_id, entity_id, value) VALUES (?, ?, ?)";
	$connection->query($sql, array($attributeId, $productId, $value));

function _updateMissingImages($count, $productId, $data){
	$connection				= _getConnection('core_read');
	$smallImageId			= _getAttributeId('small_image');
	$imageId				= _getAttributeId('image');
	$thumbnailId			= _getAttributeId('thumbnail');
	$mediaGalleryId			= _getAttributeId('media_gallery');

	//getting small, base, thumbnail images from catalog_product_entity_varchar for a product
	$sql	= "SELECT * FROM " . _getTableName('catalog_product_entity_varchar') . " WHERE attribute_id IN (?, ?, ?) AND entity_id = ? AND `value` != 'no_selection'";
	$rows	= $connection->fetchAll($sql, array($imageId, $smallImageId, $thumbnailId, $productId));
		foreach($rows as $_image){
			//check if that images exist in catalog_product_entity_media_gallery table or not
			if(!_checkIfRowExists($productId, $mediaGalleryId, $_image['value'])){
				//insert that image in catalog_product_entity_media_gallery if it doesn't exist
				_insertRow($productId, $mediaGalleryId, $_image['value']);
				/* Output / Logs */
				$missingImageUpdates = $count . '> Updated:: $productId=' . $productId . ', $image=' . $_image['value'];
				echo $missingImageUpdates.'<br />';
		$separator = str_repeat('=', 100);
		echo $separator . '<br />';
/***************** UTILITY FUNCTIONS ********************/

$messages			= array();
$csv				= new Varien_File_Csv();
$data				= $csv->getData('update_missing_images.csv'); //path to csv
$fields				= array_shift($data);
#print_r($fields); print_r($data); exit;

$message = '<hr />';
$count   = 1;
foreach($data as $_data){
	$sku									= isset($_data[_getIndex('sku')]) ? trim($_data[_getIndex('sku')]) : '';
			$productId = _getIdFromSku($sku);
			_updateMissingImages($count, $productId, $_data);
			$message .= $count . '> Success:: While Updating Images of Sku (' . $sku . '). <br />';

		}catch(Exception $e){
			$message .=  $count .'> Error:: While Upating Images of Sku (' . $sku . ') => '.$e->getMessage().'<br />';
		$message .=  $count .'> Error:: Product with Sku (' . $sku . ') does\'t exist.<br />';
echo $message;

3> Open your browser and run the following url:

4> That’s all. Try to browse the Images tab, now you will see those missing images.

Hope this helps somebody!

Posted in Latest, Magento and tagged , , , , , , , , , , , . Bookmark the permalink.

About Raj (MagePsycho)

Raj, the guy behind MagePsycho is a Zend PHP5 Certified Engineer, Magento Certified Developer, Magento Moderator / Freelancer with specialization in web applications (CMS, E-Commerce, ERP etc.). Catch him on: Twitter: @magepsycho Skype: magentopycho
  • Vincent

    I’m trying this solution but it is not working. When I run the php file in the browser, it is giving me a 500 internal server error.

    • For 500 internal server error, be sure to check error-log. You can get more info about the cause.

  • Vincent

    So when for the CSV file, do I just have one column with SKU and then list the SKUs of the products that have missing images? It still isn’t working. I’m trying this method on images that I uploaded several weeks ago.

  • Paul

    Thanks for this fix! I’m pretty sure it’ll work, but right now it doesn’t appear to. 🙁 Maybe I’m misunderstanding the use??

    Our client imports products (as a csv) with the image paths and uploads the images to the correct path. As far as I know everything works fine when they do that. HOWEVER, if they manually update a product, the images get reset to no_selection.

    When I run this code, I get all of the skus with the Success message, but nothing appears to be updated in the catalog_product_entity_varchar table. Any ideas what it could be??

  • devonshire

    this is the closest post I’ve found to solving my problem. Do you know of a way I can fix this on a global level – so that whenever I import I do not have to run this fix again? Thanks

  • I had this problem and was close to using your solution. However, with a little more checking, I realised that media_attribute_id, media_image, media_position and media_is_disabled were all not set. After re-uploading products
    (just sku plus the aforementioned columns) the issue was fixed, with images appearing in the backend and frontend successfully.

    media_image (path to your main image)
    media_attribute_id (for me was 88, get this from the table catalog_product_entity_media_gallery – it may vary for different versions.
    media_postition (1)
    media_is_disabled (0)

    Not sure if that will solve everyone’s problem, but it worked for me, without doing any of the ‘scary’ stuff above. I’m on 1.7.

    • Nitin

      Thanks a lot Nathan its really great..worked for for me perfectly…

      • P

        Thanks Nathan. I did a lot of research for the answer to be so simple. Wish I had found your response a few hours ago.

  • Claire

    Not sure if this applies to your situation, but I have just installed and tried to import a CSV with product with multiple images which I had exported from a previous 1.6.x installation. I was tearing my hair out as it was only showing one image on the front end, and no images at all on the backend. To resolve this I created a dummy product in the new install, added multiple images, exported the products out, and looked in the CSV file for the _media_attribute_id column. The number in here was 88 for the new install, but 82 for the export from the old install, so I simply updated the number, reimported the CSV from the old install again, and voila, it worked perfectly.

    • We really struggled with this too and had seen the sql code to run but saw your post and decided to give it a go first and it worked! We exported from EE into CE 1.7 and the media_attribute_id changed as per yours – from 82 to 88. So we changed this on the import and ran it again. Images now show on the front end and in admin as you can see on

  • Martin

    Thanks, Nathan! Works like a charm. However, you left the underscore off the field names. Fields should be:

    _media_image (path to your main image)
    _media_attribute_id (for me was 88, get this from the table catalog_product_entity_media_gallery – it may vary for different versions.
    _media_postition (1)
    _media_is_disabled (0)

    Run it with just these fields to fix products that were previously uploaded. Add these fields to any future product imports to avoid the missing product image in the backend.

  • Thanks It works!!!!!!! Save my day

  • Thanks Nathan,

    It worked for me

  • Thanks for posting this solution. I got half way figuring out what was going on when I decided to Google. Luckily this post was with the first few results pointing me to the cause of the problem. Thanks Claire for mentioning the possible difference in _media_attribute_id, that got my import conversion script generating working import CSV again by just updating the id 😀

  • After trying all kinds of suggested solutions, I tried yours. Thank you! This is the only one I found that really works. So many of the others mention media_attribute_id which no longer exists as an attribute in by default. It is now called media_gallery, but even then the other solutions did not work.

  • Jim

    Totally worked on 1.7.02. Just had to reindex afterwards, and BAM! Images on the backend. I’m gonna play around with Nathan’s idea too – I suspect it will work, but I tried it earlier with a failure…although I may not have reindexed afterwards?

  • Robert Bavington

    Thank you so much for this fix, I was searching hi and low for a solution for this problem and I couldn’t work out why my images were not showing in the back end. This is an excellent example of community support!

  • Sangay Tenzin

    Thank you very much for this. Wish I fount this post earlier. I have already spent one day trying to get the images on the backend after import : (

  • FrankLuska

    Thank You, Thank You, Thank You!

  • Robert Howell

    you are the man.. I managed to import all my products and the images where every where but in the product description for the admin, so if I edited the product the image would disappear from the losting as it was not there.. your mod worked perfect! I’m using Magneto CE 1.8

    • Magento Developer

      Glad it worked for you.

  • Daryl

    You beauty!

  • rrroulio57

    ouawww. 2 days with my bug. the answer here. Thanks!!!

  • Pierre

    Thanks, perfect.
    Want to update all SKU :

    $productsCollection = Mage::getModel(‘catalog/product’)->getCollection();
    foreach($productsCollection as $product) {
    $sku = $product->getSku();

    • Robert

      Hey Pierre, You posted a year ago. But any chance you remember where exactly this code belongs? I would like to update all the skus instead of the csv file skus

      • Robert

        Never mind figured it out. Line 133 needs to be replaced with Pierres code. Thanks pierre!

  • It worked great for me, thanks a lot for this fix

  • thank you kind sir! you’re the genius.

  • Marin Vassilev

    Thank you Very MUCH!!!! I lost about a whole day to try to find a solution and Boom i found yours!! Thank you , you saved me a lot of time!!! Cheers Tested on Magento 1.9

  • Worked Like a charm on Magento Thank you very much for this.. saved me a whole lot of time!

  • Alexa Herrera

    You sir are amazing. Thank you so much!! You just saved my butt! Confirmed working on CE 1.9!

    • Magento Developer

      WOW! This makes me feel sooo good 🙂

  • Payal

    Parse error: syntax error, unexpected ‘$mageFilename’ (T_VARIABLE) in /home/magento/public_html/update_missing_images.php on line 6

  • Derrick Kinsey

    Still works great over two years after original post. I slightly modified it to run using php cli to keep the script more secure. better to keep utility scripts out of the public web root in my opinion. Thanks for the great tool!

    • Magento Developer

      Please do share the CLI version if you don’t mind 😉

  • Julian Wittorf

    First off: thanks for the script, works great on Magento CE I had the problem not knowing which products were affected/messed up, what was the cause of the error. It turned out that it only effected products beeing imported, that did NOT have the column “_media_image” set. Be sure to add the images to the “_media_image” column before setting the “image”, “small_image” and “thumbnail”. Another quick note: be sure to add a heading “sku” to your csv-file.

  • Martin Leeney

    hi I’m getting the following error mage_mage_catalog_product_entity_media_gallery’
    doesn’t exist on each image update my tables are preceeded with mage_ so
    mage_catalog_product_entity_media_gallery does exist. What do i need to change to get your file to work? Thanks

    • Suresh Singh

      did you find the solution… because i am facing the same problem.. If you find the solution please let me know..

      • David A. Palmer


        $tableName = _getTableName(‘catalog_product_entity_media_gallery’);


        $tableName = ‘catalog_product_entity_media_gallery’;

        • Grietje Straver

          With this I’m getting success messages but still pictures won’t show in the backend..

          • zonkeyweb

            I know this is some time ago, but did you ever get this working? I’m having the same issue

          • Grietje Straver

            Yes, I did. Don’t ask me how. Too long ago. Sorry. 🙂

          • IanPullens

            did you figure it out?

          • zonkeyweb

            It was a conflict with the CreareSEO extension for me. There was a help article on the main Magento forums i think. I can probably dig out the link if you cant find it.

  • Hi, tried this with Magento 1.9 multisite installation after move to a new server, but did not work.

  • pauls

    Good Work Raj,,

    Code is working fine. This solution contains few things that are new for me, I wasn’t aware of.
    I use FME Photo Gallery Magento extension ( ) to manage and display product photos anywhere in the website.

    • Magento Developer

      You mean the issue occurred because of FME Photo Gallery module?

  • Muhammad Saeed Khan

    Awesome. You made my day 🙂 thanks

  • Kristian Yates

    Genius! Had this issue after cart 2 cart migration on Big up yourself, Raj!

  • leo

    Magento 1.9.2 doesnt work for me, it only loads blank page with thats all and images dont get updated. Any suggestions?

    • Jesse

      Hi, Leo! I’m getting the same problem. Could you tell me how can I fix this? Thank you!

  • Daniel Zander


  • Pankaj

    hello friends,i am also an user of magento there is an error occuring in my admin panel also that the there are approx 1000 products in my store and all products images are displaying over website but not in admin panel and also the Browse files.. and Upload files buttons are also missing…i tried all the solutions available over internet but still facing the problem…dear guys please help me to come out from this frustrating problem…

  • Pankaj

    Dear Guys i am also facing problem in magento admin panel there are nearly 1000 products in my store but not even a single product is displaying image in admin panel as well as there are two buttons namely Browse Files and Upload files are missing..i tried all the solution available on net..but still problem remains the same…the images are displaying over website but not on admin panel…please help me…feeling frustrated with this problem…