我正在尝试使用数据库中的值生成CSV文件并强行下载。它工作正常,除了文件从服务器获取(不相关的)错误消息并将它们包含在CSV文件中。我该如何防止这种情况发生?
这是我的功能:
public function export($coupons_ids) {
$coupons_ids = explode(',', (urldecode($coupons_ids)));
$result = $this->cart_coupons->get_where_in('id',array_filter($coupons_ids, 'strlen'));
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=coupons.csv');
$output = fopen('php://output', 'w');
fputcsv($output, array('id', 'name', 'start_date', 'end_date', 'promo', 'usage_limit'));
foreach ($result as $row)
{
$id = $row->id;
$name = $row->name;
$start_date = $row->start_date;
$end_date = $row->end_date;
$usage_limit = $row->usage_limit;
$active = '';
switch($row->active)
{
case 0:
$active = 'YES';
break;
case 1:
$active = 'NO';
break;
}
$promo = '$0';
switch($row->promo_type)
{
case 0:
$promo = '$' . $row->promo_value;
break;
case 1:
$promo = $row->promo_value . '%';
break;
}
$data = array('id' => $id, 'name' => $name, 'start_date' => $start_date, 'end_date' => $end_date, 'promo' => $promo, 'usage_limit' => $usage_limit);
fputcsv($output, $data);
}
fclose($output);
}
输出结果为:
<br />
<b>Strict Standards</b>: Only variables should be passed by reference in <b>D:\xampp\htdocs\----\-----.php</b> on line <b>245</b><br />
id,name,start_date,end_date,promo,usage_limit
3,Test3,"2013-03-12 00:00:00","2013-03-20 00:00:00",$500,10
4,Test4,"2013-03-17 00:00:00","2013-04-25 00:00:00",25%,50
5,Test5,"2013-03-20 00:00:00","2013-03-25 00:00:00",$40,1000
6,Test6,"2013-03-12 00:00:00","2013-03-20 00:00:00",$50,100
7,Test7,"2013-03-15 00:00:00","2013-03-28 00:00:00",10%,150
81,CC00016,"2013-04-20 00:00:00","2013-04-27 00:00:00",$100,10
80,CC00015,"2013-04-16 00:00:00","2013-04-23 00:00:00",$100,1
79,CC00014,"2013-04-16 00:00:00","2013-04-23 00:00:00",$100,1
78,CC00013,"2013-04-16 00:00:00","2013-04-23 00:00:00",$100,1
76,CC00011,"2013-04-16 00:00:00","2013-04-23 00:00:00",$100,1
77,CC00012,"2013-04-16 00:00:00","2013-04-23 00:00:00",$100,1
我知道这个错误是什么,它与代码的功能完全无关且无关紧要。任何帮助表示赞赏。
答案 0 :(得分:0)
将error_reporting(0);
放在脚本的开头,以获得快速解决方案