title: Magento - programmatically add image from url?
body: <p>I'm currently working around one script which programmatically add products.
  Right now I have problem with the part that this script gets an image from URL and upload it to a directory and then use it as a product image.</p>
  
  <p>I am using Magento 1.9.1.</p>
  
  <p>So here is the part of the script which is supposed to take the image from the URL and upload it to the new product as product image:</p>
  
  <pre><code>$product = Mage::getModel('catalog/product')-&gt;load($new_product_id);
  
  $url = 'http://media.asicdn.com/images/jpgo/6080000/6089330.jpg';
  $img = '/media/catalog/product/images/image1.jpg';
  file_put_contents($img, file_get_contents($url));
  
  $product-&gt;setMediaGallery (array('images'=&gt;array (), 'values'=&gt;array ()));
  $product-&gt;addImageToMediaGallery ($img , array ('image','small_image','thumbnail'), false, false);
  Mage::app()-&gt;setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); 
  
  $product-&gt;save();
  </code></pre>
  
  <p>When I do that I receive error:</p>
  
  <pre><code>Fatal error: Uncaught exception 'Mage_Core_Exception' with message 'Image does not exist.' in /home/superweb/public_html/sportsdirect/app/Mage.php:595 Stack trace: #0 /home/superweb/public_html/sportsdirect/app/code/core/Mage/Catalog/Model/Product/Attribute/Backend/Media.php(274): Mage::throwException('Image does not ...') #1 /home/superweb/public_html/sportsdirect/app/code/core/Mage/Catalog/Model/Product.php(1047): Mage_Catalog_Model_Product_Attribute_Backend_Media-&gt;addImage(Object(Mage_Catalog_Model_Product), '/media/catalog/...', Array, false, false) #2 /home/superweb/public_html/sportsdirect/SignProduct.php(63): Mage_Catalog_Model_Product-&gt;addImageToMediaGallery('/media/catalog/...', Array, false, false) #3 {main} thrown in /home/superweb/public_html/sportsdirect/app/Mage.php on line 595
  </code></pre>
  
  <p>The product is created but there is no image for the product.</p>