Steve
Steve

Reputation: 71

PHP fputcsv is not working, Nor creating csvfiles

By which I am exporting all products into csv file,

But my csvfile is not create, I am able to see variables output on screen.

<?php 
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
set_time_limit(0);
require_once '/var/www/html/app/Mage.php';
umask(0);

Mage::app('default');

$connection = Mage::getSingleton('core/resource')->getConnection('core_write');

define('CSV_PATH', 'var/www/html/rahul_csvfiles/'); // specify CSV file path

$csv_fileoutput = CSV_PATH . "cfd_all_products_log".date("Y-m-d H:i:s").".csv";
$csvfileoutput  = fopen($csv_fileoutput, 'a');
fputcsv($csvfileoutput, array('Entity_id','SKU','Product_Name','Product Name','Title', 'Short Description', 'Long Description', 'URL'));

$store_id = 2;
$collection = Mage::getModel('catalog/product')->getCollection()->addStoreFilter($store_id)
->addAttributeToSelect('*') 
->setPageSize(3) 
->setCurPage(1);

foreach ($collection as $product) {
  $product_id =  $product->getID();
  $sku = $product->getSKU();
  $productname =  $product->getName(); 
  $price =  (float) $product->getPrice(); 
  $description =  $product->getDescription();  
  $shortdescription = $product->getShortDescription(); 
  $url =  $product->getProductUrl();
echo $product_id. "\n";
echo $sku. "\n";
echo $productname. "\n";
echo $price. "\n";
echo $url . "\n";

fputcsv($csvfileoutput, array($product_id,$sku,$productname,$productname,$price,$description,$shortdescription,$url));
}

?>

Please let me know where I am doing wrong, Any suggestion would be helpful.

Thanks

Upvotes: 1

Views: 1137

Answers (1)

Jakub Matczak
Jakub Matczak

Reputation: 15656

If the file is not being created at all, that means it's an issue with fopen and not fputcsv.

You need some debugging here. First dump $csvfileoutput using var_dump. It's probably false, which means, that it failed to open file.

If it is indeed false, then I guess that's an issue with permissions. Check if web server's user (usually www-data if you're using Apache) has writing persmission in the path where you're trying to create file (CSV_PATH).

Upvotes: 2

Related Questions