I am doing a product import routine and have images added to my product. The problem I have is that magento creates a dispretion path and then add's some numbers onto the end of my image file name to ensure it's not a duplicate. I need to find a way to check if myimage.jpg has been added to a product or not.

I can find the dispretion path using Mage_Core_Model_File_Uploader::getDispretionPath($fileName); so that isn't a problem but from what I can see there isn't a way to map the file name I uploaded to the one magento renamed it do, for instance:

I add an image called BadgersSmall.jpg and this is what magento returns as part of the MediaGallery collection

[value_id] => 5
[file] => /B/a/BadgersSmall_6_1.jpg
[label] => 
[position] => 2
[disabled] => 0
[label_default] => 
[position_default] => 2
[disabled_default] => 0
[url] => http://www.localhost.com:8080/magento/media/catalog/product/B/a/BadgersSmall_6_1.jpg
[id] => 5
[path] => C:\xampp\htdocs\magento\media\catalog\product\B\a\BadgersSmall_6_1.jpg

The only thing I can think of at this point is running _getNotDuplicatedFilename($fileName, $dispretionPath); on my current image and then checking the index at the end of the file, subtract 1 and that "should" be the previous file I uploaded but this solution is simply horrible.

EDIT My new plan is to create a database table that will hold my product code, file name and the file name magento creates. I will insert this when I add new images. I will also need to add something in to observe the delete image event so I can remove the entry as well.

Any ideas?

  • This is not actually an answer, it's more of a suggestion. I thing you can use a regular expression to check it the image name is similar to the one you are trying to attach, and for the images that match the regex, check the file size. It they are the same the chance is high that they are the same image.
    – Marius
    Jun 7, 2013 at 8:10
  • Yeah you can compare filesize and then if erqual first 1000 bytes of the files (for better performance, not to load whole files contents) Nov 9, 2013 at 19:04
  • did you manage to make any progress on this? May 17, 2014 at 19:03
  • @DavidManners - I did actually sort this out and find a way to check if the image is set. It's not done entirely by using Magento functions but I wrote some to compare the files. When I get to work on Monday, I'll post my code to show how I got around it.
    – webnoob
    May 17, 2014 at 22:35
  • @webnoob can you please add your solution as an answer as it may help other people who face the same issue? Jun 15, 2014 at 10:03

1 Answer 1


Unfortunately, we had to hack this a little bit as I couldn't find a reliable way to determine if an image is in use on a product or not. This will handle "most" image names but it will fail on images ending with _{NUMBER} i.e _1.

private function getImageWithoutMagentoBits($fileName)
    $ext = "." . pathinfo($fileName, PATHINFO_EXTENSION);
    $fileName = str_replace($ext, " " . $ext, $fileName);
    $withoutExt = str_replace($ext, "", $fileName);
    $magentoCount = strrchr($withoutExt, "_");

    if(is_numeric(str_replace("_", "", str_replace(" ", "",$magentoCount))))
        $result = str_replace($magentoCount, "", $fileName);
        $result = str_replace(" ", "", $fileName);

    if (is_numeric(str_replace("_", "", trim(str_replace($ext, "", strrchr($result, "_"))))))
        return $this->getImageWithoutMagentoBits($result);

    return $result;

This will run through the file name of the image and remove all the magento bits. Magento can have an image like 'myimage_1_2_3.jpgormyimage_1.jpg`.

As I say, it's more of a hack than a solution but it's working for us at the moment.

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.