【问题标题】:fputcsv causing "Headers Already Sent" errorfputcsv 导致“标头已发送”错误
【发布时间】:2014-04-11 20:56:10
【问题描述】:

我正在为我的 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); 出于某种原因,当注释掉这一行时,脚本运行良好。但是,当此行与脚本一起运行时,它会触发标头已发送错误。我不明白为什么我可以在我的 foreach 循环中多次运行 fputcsv (fputcsv($fp, $row);),但我根本无法在 foreach 循环之前运行它。

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

感谢您的宝贵时间!

【问题讨论】:

    标签: php magento csv export fputcsv


    【解决方案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);
                    }
    ?>
    

    【讨论】:

    • 非常感谢您编写此版本!我应该使用 Varien_Io_File,但我之前没有听说过。话虽如此,我真的希望我知道我发布的脚本出了什么问题。我有此代码的工作版本,但我想了解它失败的地方。
    • 唯一导致标头已发送错误的 fputcsv 是我的代码中标头列的 fputcsv。 foreach 循环中的 fputcsv 不会给我标头已发送错误。出于这个原因,我不相信 mage.php 在我的文件之前发送标头 - 或者 foreach 也会给我错误。有什么想法吗?
    猜你喜欢
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 2019-08-29
    • 1970-01-01
    • 2011-07-04
    • 2015-06-17
    • 1970-01-01
    • 2014-01-08
    相关资源
    最近更新 更多