Magento Image Upload Through Custom CSV Created By Custom Script

This magento tutorial explains about creating a custom CSV file which will be used for uploading to Magento by the use of profiler in System->Dataflow Proflie in admin.

<?php

/*******Script used to import Product images from exported custom CSV Sheet******/

include_once ‘app/Mage.php’;
Mage::app();
ini_set(‘memory_limit’, ‘2048M’);
ini_set(‘max_execution_time’, 0);
ini_set(‘display_errors’, 1);
error_reporting(E_ALL);
try {
$baseFilePath = Mage::getBaseDir().’/var/import’;
$baseMediaPath = Mage::getBaseDir().’/media/import’;
if (!file_exists($baseFilePath)) {
mkdir($baseFilePath, 0777, true);
}

$csvToCreate = $baseFilePath . ‘/import_all_product_images.csv’;
$csvObj = new Varien_File_Csv();
$csvData = array();
$productHeader = array(‘sku’,’image’, ‘small_image’, ‘thumbnail’);
$csvData[] = $productHeader;
$imageDetails = array();
foreach(glob(“$baseMediaPath/*.*”) as $file){
$filesSplit = explode(‘media/import/’,$file);
$sku = explode(‘.’,$filesSplit[1])[0];
$file = ‘/’.strtolower($filesSplit[1]);
$imageDetails[‘sku’] = $sku;
$imageDetails[‘image’] = $file;
$imageDetails[‘small_image’] = $file;
$imageDetails[‘thumbnail’] = $file;
$csvData[] = $imageDetails;
}
$csvObj->saveData($csvToCreate, $csvData);

echo ‘CSV for images has been created.<br/>’;
} catch(Exception $e){
echo $e->getMessage();
}
?>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s