1

I am creating products and adding images to them using

$configurable->addImageToMediaGallery($filePath, array('image', 'small_image', 'thumbnail'), false);

This works fine. However I am getting files save in the media folder with names like:

/media/catalog/product/w/w/wwwhatever_2_4_1_2_1_3_1.png

After a few test runs the proliferation of these media files is jolly alarming. I am also attaching the files to the simple products using the same technique as for the configurable.

I have tried setting $uploader->setAllowRenameFiles(false); in various places, which I thought might fix the problem. But that didn't work.

I am looking for a solution that ideally enables the file renaming to work as normal during normal admin operation, e.g. for CMS files etc., but breaks the rule just for my product creation module.

  • 2
    Do you want to overwrite the old files? Magento renames the files, because they already exist. What is the intended behaviour? – Fabian Blechschmidt Apr 9 '14 at 9:10
  • Yes please! All of my imported filenames are unique (based on SKU) so I definitely want them to be over-written. This is not necessarily the case for CMS files etc., just products from my import module. (Thanks for asking!) – Theodores Apr 9 '14 at 9:31
  • see, when you are uploading the same file again and again, magento starts to rename it. If you do not want this functionality, then what has to be done in the step of renaming? – Pavan Kumar Apr 10 '14 at 7:07
2

Have a look at the function _getUniqueFileName in the class Mage_Catalog_Model_Product_Attribute_Backend_Media I think this is the function that renames files if the already exist.

It seems to use the Varien_File_Uploader::getNewFileName which checks the given file if there is already one matching. If there is then it updates the filename by 1 and tries again.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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