How to import product images from external URL in Magento


Generally, we import the product images by copying all the images to /media/import folder, adding image path relative to /media/import in CSV corresponding to the product SKUs and running the import profile.

Sometimes you may need to import images from external URL and this is the case when you are using third-party web services or migrating non-magento cart to Magento.


Suppose say we have a CSV with an extra field: external_image_url for product import.


Copy app/code/core/Mage/Catalog/Model/Convert/Adapter/Product.php to the local code pool so that new path looks like:


Open the Product.php from local code pool and search for the following code within saveRow() function:

foreach ($imageData as $file => $fields) {
	try {
		$product->addImageToMediaGallery(Mage::getBaseDir('media') . DS . 'import' . trim($file), $fields);
	catch (Exception $e) {}

Add the following code just below it:

$image_url  = $importData['external_image_url']; //get external image url from csv
$image_type = substr(strrchr($image_url,"."),1); //find the image extension
$filename   = md5($image_url . $importData['sku']).'.'.$image_type; //give a new name, you can modify as per your requirement
$filepath   = Mage::getBaseDir('media') . DS . 'import'. DS . $filename; //path for temp storage folder: ./media/import/
file_put_contents($filepath, file_get_contents(trim($image_url))); //store the image from external url to the temp storage folder
$mediaAttribute = array (
 * Add image to media gallery
 * @param string        $file              file path of image in file system
 * @param string|array  $mediaAttribute    code of attribute with type 'media_image',
 *                                         leave blank if image should be only in gallery
 * @param boolean       $move              if true, it will move source file
 * @param boolean       $exclude           mark image as disabled in product page view
$product->addImageToMediaGallery($filepath, $mediaAttribute, false, false);


That’s all. Now you can proceed with normal dataflow import profile.

From above code it is clear that we have used the php functions: file_get_contents() & file_put_conents() for the purpose.
Be sure to check if the fopen wrappers have been enabled or not in order to use URL as a parameter for file_get_contents().

Refer the configuration directive: allow-url-fopen for more info.

Let me know your feedback regarding this article.

Thanks for reading.

EDIT: FYI, this code was tested in Magento 1.5.x version.