0

I am updating product's attributes and images through program. The problem is that, it is adding new images everytime and appending _1 or _1_1 to the file. Hence a lot of duplicate files in the drive.

I searched in the forum for the solution. They say not to save the product twice. You can see below code that I have a function downloadAndImportImage() that adds images and later on I save the product.

What am I missing ? Thanks in advance.

function downloadAndImportImage($image_url, $_product, $num, $sku){

    try{
        .
        .
        .

        if (file_exists($filepath_to_image)) {
            $_product->addImageToMediaGallery($filepath_to_image, array('image', 'small_image', 'thumbnail'), false, false);

        }
    }catch(Exception $e){
        echo $e->getMessage();
    }
}


function updateProduct(){
...
if($record[14]){
    if ($record[14] != $record[13]){
       downloadAndImportImage($record[14], $_product, 14, $record[0]);
   }
}
if($record[15]){
   if ($record[14] != $record[15]){
      downloadAndImportImage($record[15], $_product, 15, $record[0]);
 }
}

$_product->save();

}
1
  • Hi there, check the answer & give feedback ;) Jun 28, 2018 at 10:10

1 Answer 1

0

You'd validate if current media values of product are the same as the ones you are trying to update. If same file exists Magento will append that _1 to filename to avoid overwriting files. This happens in lib/Varien/File/Uploader.php

static public function getNewFileName($destFile)
{
    $fileInfo = pathinfo($destFile);
    if (file_exists($destFile)) {
        $index = 1;
        $baseName = $fileInfo['filename'] . '.' . $fileInfo['extension'];
        while( file_exists($fileInfo['dirname'] . DIRECTORY_SEPARATOR . $baseName) ) {
            $baseName = $fileInfo['filename']. '_' . $index . '.' . $fileInfo['extension'];
            $index ++;
        }
        $destFileName = $baseName;
    } else {
        return $fileInfo['basename'];
    }

    return $destFileName;
}

You can try changing that logic & use your custom method, rewriting _getNotDuplicatedFilename() method in Mage_Catalog_Model_Product_Attribute_Backend_Media class, which calls above function

Another way would be running some clean process after your update, and delete unused images. You have some examples here

https://gist.github.com/aleron75/07ab2a950b2e3429a820

https://magento.stackexchange.com/a/214255/3566

1
  • For now I am checking if an image with particular name is present or not. If it is present, I am simply not uploading it. Problem with this --> consider I have uploaded an image with name SKU1.png (from any external link), when I try reupload different image with SKU1.png name, it will not upload according to this logic. If I let upload images upload again and use cleanups, I might have to increase my storage only (~6GB images upload in one time). Cleanup process looks better.
    – Raj Kumar
    Jun 28, 2018 at 11:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.