Archive for February, 2010

 

Import images into Magento databse without using Magento core files.

Friday, February 12th, 2010

here code is to import images into magento databse without using any core files.
this is for product’s images. you can modify it for gallery images also.
here all the images took from media/import and copied to that perticular directory.

you can find here,how images created there directory structure.

if images name is example.gif,then will create directory structure like e/x/example.gif.
that is means this images will copy to media/catalog/product/e/x/example.gif
and the value /e/x/example.gif will insert into table catalog_product_entity_varchar.you can see this image at fronend to that perticular product.Actaully i did script to imaport product with catagory,attribute,images,description…. etc
it is nothing but a cron job.i took only import images part from my script.

$LargeProductImageURL=$LargeImageURL!=”"?basename($allData[$key]->LargeImageURL):”";

$first_2_letter = substr(“$LargeProductImageURL”, 0,2);
$first_letter = substr(“$first_2_letter”, 0,1);
$second_letter = substr(“$first_2_letter”, -1);
$product_image =”/”.$first_letter.”/”.$second_letter.”/”.$LargeProductImageURL;

$product_path=$media_path.”".$slash.”catalog\product”;
$import_images=$media_path.”".$slash.”import”.$slash.”".$LargeProductImageURL;
$mypath1=$product_path.”".$slash.”".$first_letter;
if(!is_dir($mypath1))
{
mkdir($mypath1,0777,TRUE);
}
$mypath2=$product_path.”".$slash.”".$first_letter.”".$slash.”".$second_letter;
if(!is_dir($mypath2))
{
mkdir($mypath2,0777,TRUE);
}
//copy ( string $source , string $dest [, resource $context ] )
$Image_dest=$mypath2.”".$slash.”".$LargeProductImageURL;
@copy($import_images,$Image_dest);

#####check Is images are ready for copy or not
if (file_exists($import_images)) {
$product_image = $product_image;
}
else {
$product_image=”no_selection”;
}
#######
$UrlKey = strtolower(str_replace(” “,”-”,$PName));
$URL_path= $PName.”.html”;

/*if(empty($LargeProductImageURL))
{$product_image=”no_selection”;}*/

$db_obj->query(“INSERT INTO catalog_product_entity_varchar(entity_type_id, attribute_id,store_id,entity_id,value) VALUES
(‘”.$catalog_product.”‘,’56′,’0′,’”.$entity_id.”‘,’”.$PName.”‘),
(‘”.$catalog_product.”‘,’82′,’0′,’”.$entity_id.”‘,’”.$UrlKey.”‘),
(‘”.$catalog_product.”‘,’469′,’0′,’”.$entity_id.”‘,’2′),
(‘”.$catalog_product.”‘,’67′,’0′,’”.$entity_id.”‘,’”.$PName.”‘),
(‘”.$catalog_product.”‘,’69′,’0′,’”.$entity_id.”‘,’”.$PName.”‘),
(‘”.$catalog_product.”‘,’70′,’0′,’”.$entity_id.”‘,’”.$product_image.”‘),
(‘”.$catalog_product.”‘,’71′,’0′,’”.$entity_id.”‘,’”.$product_image.”‘),
(‘”.$catalog_product.”‘,’72′,’0′,’”.$entity_id.”‘,’”.$product_image.”‘),
(‘”.$catalog_product.”‘,’86′,’0′,’”.$entity_id.”‘,”),
(‘”.$catalog_product.”‘,’90′,’0′,’”.$entity_id.”‘,”),
(‘”.$catalog_product.”‘,’92′,’0′,’”.$entity_id.”‘,’container2′),
(‘”.$catalog_product.”‘,’95′,’0′,’”.$entity_id.”‘,”),
(‘”.$catalog_product.”‘,’96′,’0′,’”.$entity_id.”‘,”),
(‘”.$catalog_product.”‘,’97′,’0′,’”.$entity_id.”‘,”),
(‘”.$catalog_product.”‘,’83′,’0′,’”.$entity_id.”‘,’”.$URL_path.”‘)”, $db_conect);

regard
Manoj Ninave

How to Unzip/Extract zip file into one directory.

Wednesday, February 3rd, 2010

ini_set(‘memory_limit’,’800M’);
set_time_limit(36000);
/**
* @Module to Extract folder in one directory
* @package general
* @copyright Copyright 2010-11 COG IT Solutions Development Team
* @Author: Mr. Manoj M.Ninave
**/
//create media/import directory in root dir
$dir = $_SERVER['DOCUMENT_ROOT'].”/media/import”;
$filename = $dir.”/ABC.zip”;//destination
$zip_file = “http://www.yourssite.com/ABC.zip”;//source

$contents = file_get_contents($zip_file);
//echo $contents;
$handle = fopen($filename, “w+”);
fwrite($handle,$contents);
fclose($handle);
$zip = zip_open($filename);
if ($zip)
{
while ($zip_entry = zip_read($zip))
{
$zipEntry = zip_entry_name($zip_entry);
$zipEntryFile = substr($zipEntry,strrpos($zipEntry,”/”)+1);
$dirFile = $zipEntryFile;//unzipped directory
$dirPath = $dir.”/”.$dirFile;
$fp = fopen($dirPath, “w+”);
if (!$fp)
continue;
if(zip_entry_open($zip,$zip_entry, “r”))
{
$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
fwrite($fp,$buf);
zip_entry_close($zip_entry);
fclose($fp);
}
}
zip_close($zip);
}
unlink($filename);
//unlink($dirPath);
echo “Unzipped successfully.”;

First it will copy zip file from source to destination.
All the files in subdirectory will extract in one directory i.e. in media/import directory.

Regard

Manoj Ninave

Software Engineer,
COG IT Solutions Pvt. Ltd.
www.cogitsolutions.com
n.manoj@cogitsolutions.com

Site map | Blog
Copyright © 2008, COG IT Solutions Pvt. Ltd. All Rights Reserved
XHTML Validated