fputcsv causing “Headers Already Sent” error

旧巷老猫 提交于 2020-01-14 04:54:28

问题


I am writing a PHP export script for my copy of magento. For some reason, the following code gives me a "Headers already sent" error:

//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;
    }
}

The line of code here that is causing me problems is this: fputcsv($fp, $headerRow); For some reason, when this line is commented out the script runs fine. However, when this line is run with the script, it shoots the header already sent error. I don't understand why I am able to run fputcsv INSIDE my foreach loop any number of times (fputcsv($fp, $row);)but I can not run it before the foreach loop at all.

I have ways around this issue, so it's not super critical, but I really wish I could understand what was going on here to cause this.

Thanks for your time!


回答1:


i have change your code ... check this.I have using magento process to export...

<?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);
                }
?>


来源:https://stackoverflow.com/questions/23022416/fputcsv-causing-headers-already-sent-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!