fputcsv导致"标题已发送"错误

时间:2014-04-11 20:56:11

标签: php magento csv export fputcsv

我正在为我的magento副本编写PHP导出脚本。出于某种原因,以下代码给出了“已发送标头”错误:     

//Load magento and set to match frontend
require_once '../../app/Mage.php';
umask(0);
Mage::app();
Mage::app()->loadArea(Mage_Core_Model_App_Area::AREA_FRONTEND);

//Send headers to browser to prep for csv file download
header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename=exportSKUs.csv');


//Load only product collection details that we need.
$product    = Mage::getModel('catalog/product');
$product->getCollection()->getSelect()->limit(5);
$products   = $product->getCollection()
    ->addFieldToFilter('status','1')
    ->addAttributeToSelect('name')
    ->addAttributeToSelect('sku')
    ->addAttributeToSelect('upc')
    ->addAttributeToSelect('status')
    ->addAttributeToSelect('price')
    ->addAttributeToSelect('special_price')
    ->addAttributeToSelect('description')
    ->addAttributeToSelect('category_ids')
    ->addAttributeToSelect('short_description');

//Open current output to fputcsv
$fp = fopen('php://output', 'w');

//CSV headers
$headerRow = array(
    'name',
    'sku',
    'upc',
    'status',
    'price',
    'special_price',
    'description',
    'category_ids',
    'short_description'
);
fputcsv($fp, $headerRow);
$count = 0;
//CSV Rows
foreach($products as &$product){
    $categoryIds = implode(',', $product->getCategoryIds());
    $row = array(
        $product->getName(),
        $product->getSku(),
        $product->getUpc(),
        $product->getStatus(),
        $product->getPrice(),
        $product->getSpecialPrice(),
        $product->getDescription(),
        $categoryIds,
        $product->getShortDescription()
    );
    fputcsv($fp, $row);
    $count++;
    if($count>5){
        //Close current output (save csv)
        fclose($fp);
        exit;
    }
}

这里引起我问题的代码行是这样的:fputcsv($fp, $headerRow); 出于某种原因,当该行被注释掉时,脚本运行正常。但是,当使用脚本运行此行时,它会发送已发送的标头错误。我不明白为什么我能够运行fputcsv INSIDE我的foreach循环任意次(fputcsv($fp, $row);)但是我无法在foreach循环之前运行它。

我有解决这个问题的方法,所以这不是超级关键,但我真的希望我能理解这里发生的事情。

谢谢你的时间!

1 个答案:

答案 0 :(得分:1)

我已经更改了你的代码...检查一下。我使用magento进程导出...

<?php
//Load magento and set to match frontend
require_once '../../app/Mage.php';
umask(0);
Mage::app();
Mage::app()->loadArea(Mage_Core_Model_App_Area::AREA_FRONTEND);

//Send headers to browser to prep for csv file download
//header('Content-Type: text/csv');
//header('Content-Disposition: attachment;filename=exportSKUs.csv');
//
$filename="exportSKUs.csv";


//Load only product collection details that we need.
$product    = Mage::getModel('catalog/product');
$product->getCollection()->getSelect()->limit(5);
$products   = $product->getCollection()
    ->addFieldToFilter('status','1')
    ->addAttributeToSelect('name')
    ->addAttributeToSelect('sku')
    ->addAttributeToSelect('upc')
    ->addAttributeToSelect('status')
    ->addAttributeToSelect('price')
    ->addAttributeToSelect('special_price')
    ->addAttributeToSelect('description')
    ->addAttributeToSelect('category_ids')
    ->addAttributeToSelect('short_description');


 $io = new Varien_Io_File();
                $path = Mage::getBaseDir('var') . DS . 'export' . DS;
                $name = md5(microtime());
                $file = $path . DS . $name . '.csv';
                $io->setAllowCreateFolders(true);
                $io->open(array('path' => $path));
                $io->streamOpen($filename, 'w+');
                $io->streamLock(true);
                $headerRow = array(
                    'name',
                    'sku',
                    'upc',
                    'status',
                    'price',
                    'special_price',
                    'description',
                    'category_ids',
                    'short_description'
                );
                $io->streamWriteCsv($headerRow);

                    foreach($products as &$product){
                    $categoryIds = implode(',', $product->getCategoryIds());
                    $row = array(
                    $product->getName(),
                    $product->getSku(),
                    $product->getUpc(),
                    $product->getStatus(),
                    $product->getPrice(),
                    $product->getSpecialPrice(),
                    $product->getDescription(),
                    $categoryIds,
                    $product->getShortDescription()
                    );
                    $io->streamWriteCsv($row);
                }
?>