问题描述
我正在为我的magento的副本写一个PHP导出脚本。由于某些原因,下面的代码给我一个标题已发送的错误:
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;
}
}
导致问题的代码行这个: fputcsv($ fp,$ headerRow);
由于某些原因,当这行被注释掉脚本运行正常。但是,当此行与脚本一起运行时,它会拍摄标头已发送的错误。我不明白为什么我能够运行fputcsv INSIDE我的foreach循环任何次数( fputcsv($ fp,$ row);
),但我不能运行在所有的foreach循环之前。
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.
感谢您的时间!
推荐答案
代码...检查这个。我使用magento进程导出...
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);
}
?>
这篇关于fputcsv导致“头已经发送”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!