Magento PHP Script to export products data into CSV

If you ever had an issue with exporting complete products from Magento Backend due to memory allocation error or script time out error? especially if you are using a shared server.

Below PHP script can help you to export complete data into CSV format.

<?php
	ini_set('memory_limit','2048M');
	ini_set('max_execution_time',3600); 
    require_once "../app/Mage.php";
	Mage::app("default");
	$_helper = Mage::helper('catalog/output');
	$helper = Mage::helper('shoppersettings/image');
	$imgX = 252;
	$imgY = $helper->calculateHeight($imgX);
	$filename = 'export_all_products.csv'; //output will write on this file and save into your directory where this code upload and run
//on below line specify your attribute name need to export. It may be different from below. This will export all products simple and configurable
	$_productCollection = Mage::getModel("catalog/product")->getCollection()->addAttributeToSelect(array('name', 'price','special_price','brand','color','size','lingerie_type', 'image', 'status','short_description','description','sku'));
	//->setCurPage(7)   //set paging
  	//->setPageSize(5000); //set number of records you want to export
	$file = fopen($filename, 'w');     
	foreach ($_productCollection as $_product):
   		$name= $_product->getName();
		$name=str_replace("&","&amp;",$name);
		$sku=$_product->getSku();
		$text=explode(" ",$sku);
		$sku=$text[0];
		$url_key=$_product->getProductUrl();
		$image_url=Mage::getModel('catalog/product_media_config')->getMediaUrl( $_product->getImage());  // get correct image path, not from cache
		if($_product->getSpecialPrice()!=""):
			$price=number_format($_product->getSpecialPrice());
		else:
			$price=number_format($_product->getPrice());
		endif;
		$desc=strip_tags($_product->getShortDescription());
		$desc=str_replace("&nbsp;"," ",$desc);
		$desc=str_replace("&","&amp;",$desc);
		$desc1=strip_tags($_product->getDescription());
		$desc1=str_replace("&nbsp;"," ",$desc1);
		$desc1=str_replace("&","&amp;",$desc1);
		$id=$_product->getId();
                //get parent id of a child product
		$parent_ids = Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($_product->getId());
$parent_collection = Mage::getResourceModel('catalog/product_collection')
    ->addFieldToFilter('entity_id', array('in'=>$parent_ids))
    ->addAttributeToSelect('sku','entity_id');
$parent_skus = $parent_collection->getColumnValues('sku');
$entity_ids = $parent_collection->getColumnValues('entity_id');
		if($parent_skus[0]!=""):
			$parent_sku=$parent_skus[0];
			$pSku=$parent_skus[0];
			$pId=$entity_ids[0];
			$brand = Mage::getModel('catalog/product')->load($pId)->getAttributeText('brand');
			$url_key = Mage::getModel('catalog/product')->load($pId)->getProductUrl();
			$brand=str_replace("&","&amp;",$brand);
			$image_url=Mage::getModel('catalog/product')->load($pId)->getImageUrl();
			$lingerie_type = Mage::getModel('catalog/product')->load($pId)->getAttributeText('lingerie_type');
			$lingerie_type=str_replace("&","&amp;",$lingerie_type);
		else:
			$pSku="";
			$brand="";
			$lingerie_type="Lingerie";
		endif;
		$color=$_product->getAttributeText('color');
		$size=$_product->getAttributeText('size');
		$qty=5;
		$availability="In stock";
		$category="Apparel &amp; Accessories &gt; Clothing";
      $data[] = array($id,$sku,$name,$url_key,$image_url,$price,$desc,$desc1,$pSku,$brand,$lingerie_type,$color,$size,$availability,$qty,$category);   
	   endforeach;           
      fputcsv($file, array('ID','SKU', 'Name', 'URL Key','Image','Price','Short Description','Description','Parent SKU','Brand','Lingerie Type','Color','Size','Availability','Quantity','Category'));      
   foreach($data as $row){
	   fputcsv($file, $row);      
   }
   echo "Done";
	?>