Magento – re-importing of products

I have a script that pulls in data from a 3rd party file. My import simply parses and inserts rows, which is working fine.

The problem comes with images.

When the import script runs, it first deletes all the current items and then the import begins, inserting all products and images into the gallery.

On the first import, everything is fine, the images go in and I see them on the frontend no problem. The problem comes with everytime I then re-import these products, it doesn’t seem to delete all images, as when the products re-import, I see, for example the 4 images correct, then then loads of blank rows, like images should be there, but can’t be found.

I don’t want to see these blank lines, but I’m not sure why they are there.

Could it be because the images for the product are already in the catalogue?

I am really unsure what and why this is doing what it is.



My code is:

$app = Mage::app('default');
$product = Mage::getSingleton('catalog/product');

$txt_file    = file_get_contents('test.txt');
$rows        = explode("n", $txt_file);

foreach($rows as $row => $data)
//get row data
$row_data = explode('^', $data);

$info[$row]['uniqueid']         = $row_data[0];
$info[$row]['client']           = $row_data[1];
$info[$row]['make']             = $row_data[2];
$info[$row]['model']            = $row_data[3];
$info[$row]['adtext']           = $row_data[4];

//display images
$row_images = explode(',', $info[$row]['picturereference']);

foreach($row_images as $row_image)
    $product->addImageToMediaGallery(Mage::getBaseDir('media') . DS . 'import/' . $row_image, array('image', 'small_image','thumbnail'), false, false);


    try {
  echo "Saved";
    catch (Exception $ex) {
      echo "<pre>".$ex."</pre>";


Is this because the addImageToMediaGallery is called on each iteration and adding all images to each product?


3 thoughts on “Magento – re-importing of products”

  1. Ok so I figured out my problem

    Inside the foreach I moved the call to the getSingleton and I added the the following: $product = Mage::getModel('catalog/product');

    I then, after each iteration, unset the following:


    This seemed to fix my script and now imports each products images into the proper product rather than importing other and adding random blank entries

    Thanks all

  2. A few files you’ll want to examine to break down addImageToMediaGallery and determine what exactly its doing.

    1. app/code/core/Mage/Catalog/Model/Product.php – Contains the method your using, breaking it down more you’ll find…
    2. app/code/core/Mage/Catalog/Model/Product/Attribute/Backend/Media.php – Contains more of the “bits and pieces” of addImageToMediaGallery like addImage etc.

    Some possibilities to try would be either

    A) determine if the files already exist and are attached to the product and ignoring them upon a second round of import. Perhaps looking for different file stamp. getMediaGalleryImages within Product.php.

    B) clear the media files associated with the products BEFORE importing again? clearMediaAttribute and removeImage within Media.php.

    I would also try the $move option set to true within your addImageToMediaGallery call as well.

     * Add image to media gallery
     * @param string        $file              file path of image in file system
     * @param string|array  $mediaAttribute    code of attribute with type 'media_image',
     *                                         leave blank if image should be only in gallery
     * @param boolean       $move              if true, it will move source file
     * @param boolean       $exclude           mark image as disabled in product page view
    public function addImageToMediaGallery($file, $mediaAttribute=null, $move=false, $exclude=true)

    Another option would be to try to specify null for the second param, if you note the PHPDoc comments, leaving it blank will only be for the gallery which sounds like what you are wanting…

    foreach($row_images as $row_image)
        $product->addImageToMediaGallery(Mage::getBaseDir('media') . DS . 'import/' . $row_image, null, false, false);

    Let me know if any of these help.

  3. Give something like this a try:

    //Check if gallery attribute exists then remove all images if it exists
    //Get products gallery attribute
    $attributes = $product->getTypeInstance()->getSetAttributes();          
    if (isset($attributes['media_gallery'])) {
      $gallery = $attributes['media_gallery'];
      //Get the images
      $galleryData = $product->getMediaGallery();                               
      foreach($galleryData['images'] as $image){
        //If image exists
        if ($gallery->getBackend()->getImage($product, $image['file'])) {
          $gallery->getBackend()->removeImage($product, $image['file']);

    This blog posting may also help, as its where I got the code snippet form also:

Leave a Reply

Your email address will not be published. Required fields are marked *