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.

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

include_once 'app/Mage.php';
ini_set('memory_limit', '2048M');
ini_set('max_execution_time', 0);
ini_set('display_errors', 1);
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();

Leave a Reply