title: Overwrite file names when uploading catalog images to Magento
body: <p>When you upload a product image to Magento that has the same name as an existing image, normal behavior for Magento is to append a "_1" to the end of the new file name. So if I have an image named "myFile.png" and upload a second image to the same product with that same name, Magento will rename it to "myFile_1.png".  I would like to eliminate this renaming and have files of the same name replace an existing file. </p>
  
  <p>I think I found the relevant controller at: app/code/core/Mage/Adminhtml/controllers/Catalog/Product/GalleryController.php:</p>
  
  <pre><code>public function uploadAction()
  {
      try {
          $uploader = new Mage_Core_Model_File_Uploader('image');
          $uploader-&gt;setAllowedExtensions(array('jpg','jpeg','gif','png'));
          $uploader-&gt;addValidateCallback('catalog_product_image',
              Mage::helper('catalog/image'), 'validateUploadFile');
          $uploader-&gt;setAllowRenameFiles(true);
          $uploader-&gt;setFilesDispersion(true);
          $result = $uploader-&gt;save(
              Mage::getSingleton('catalog/product_media_config')-&gt;getBaseTmpMediaPath()
          );
  
          Mage::dispatchEvent('catalog_product_gallery_upload_image_after', array(
              'result' =&gt; $result,
              'action' =&gt; $this
          ));
  
          /**
           * Workaround for prototype 1.7 methods "isJSON", "evalJSON" on Windows OS
           */
          $result['tmp_name'] = str_replace(DS, "/", $result['tmp_name']);
          $result['path'] = str_replace(DS, "/", $result['path']);
  
          $result['url'] = Mage::getSingleton('catalog/product_media_config')-&gt;getTmpMediaUrl($result['file']);
          $result['file'] = $result['file'] . '.tmp';
          $result['cookie'] = array(
              'name'     =&gt; session_name(),
              'value'    =&gt; $this-&gt;_getSession()-&gt;getSessionId(),
              'lifetime' =&gt; $this-&gt;_getSession()-&gt;getCookieLifetime(),
              'path'     =&gt; $this-&gt;_getSession()-&gt;getCookiePath(),
              'domain'   =&gt; $this-&gt;_getSession()-&gt;getCookieDomain()
          );
  
      } catch (Exception $e) {
          $result = array(
              'error' =&gt; $e-&gt;getMessage(),
              'errorcode' =&gt; $e-&gt;getCode());
      }
  
      $this-&gt;getResponse()-&gt;setBody(Mage::helper('core')-&gt;jsonEncode($result));
  }
  </code></pre>
  
  <p>I have tried setAllowedRenameFiles() to false. I have also tried commenting it out. But neither of these seems to give the desired results. </p>
  
  <p>Any thoughts on how I can enable Magento to overwrite existing files of the same name?</p>