title: Magento - How to set media gallery images programmatically?
body: <p>I try to set a new magento product image e.g. <strong>che_3.png</strong> with:</p>
  
  <pre><code>$visibility = array (
     'thumbnail',
     'small_image',
     'image'
  );
  $product-&gt;addImageToMediaGallery( $file, $visibility, true, false);
  </code></pre>
  
  <p>The product image is moved from the</p>
  
  <p>temporary directory: <strong>/media/tmp/catalog/product/upload/sessionid/original/che_3.png</strong></p>
  
  <p>to the</p>
  
  <p>destination directory: <strong>/media/catalog/product/c/h/che_3.png</strong></p>
  
  <p>Now, if i want to call the image filepath - that is saved by magento - is wrong. The media_gallery object looks like this:</p>
  
  <pre><code>[media_gallery] =&gt; Array
          (
              [images] =&gt; Array
                  (
                      [0] =&gt; Array
                          (
                              [value_id] =&gt; 89
                              [file] =&gt; /c/h/che_3_2_1.png //che_3.svg is already in here
                              [label] =&gt; 
                              [position] =&gt; 1
                              [disabled] =&gt; 0
                              [label_default] =&gt; 
                              [position_default] =&gt; 1
                              [disabled_default] =&gt; 0
                          )
  
                      [1] =&gt; Array // why this second item?
                          (
                              [value_id] =&gt; 88
                              [file] =&gt; /c/h/che_3_2.png
                              [label] =&gt; 
                              [position] =&gt; 1
                              [disabled] =&gt; 0
                              [label_default] =&gt; 
                              [position_default] =&gt; 1
                              [disabled_default] =&gt; 0
                          )
  
                  )
  
              [values] =&gt; Array
                  (
                  )
  
          )
  </code></pre>
  
  <p>As you can see the filename is extended by magento and also two items are added to the media_gallery.</p>
  
  <p>My question is how can I reset the product respectively the internal counter so magento is using the original file as it is?</p>
  
  <p>Thanks in advance.</p>