无法修改静态功能上的标题信息 [英] Cannot modify header information on Static Function
问题描述
我想要做的是调用一个类将mysql数据导出到另一个类的excel中。下面是我用过的代码(我省略了打开连接的代码):
//这是用于调用`ExportToExcel`类的类
类SampleClass {
public static function mainFunction(){
ExportToExcel :: exportToExcel();
类ExportToExcel {
public static function exportToExcel(){
$ DB = DB :: Open(); //打开数据库
$ csv_terminated =\\\
;
$ csv_separator =,;
$ csv_enclosed ='';
$ csv_escaped =\\;
$ sql_query =查询到这里......;
$ result = $ DB-> qry($ sql_query,2,TRUE); //运行查询
$ fields_cnt = 0;
$ schema_insert ='';
while($ finfo = mysqli_fetch_field($ result)){
$ fields_cnt ++;
$ l = $ csv_enclosed。str_replace($ csv_enclosed,$ csv_escaped。$ csv_enclosed,stripslashes($ finfo-> ; name))。$ csv_enclosed;
$ schema_insert。= $ l;
$ schema_insert。= $ csv_separator;
}
$ out = trim(substr( $ $ $ $。$ = $ csv_terminated;
while($ row = mysqli_fetch_array($ result)){
$ schema_insert ='' ;
for($ j = 0; $ j <$ fields_cnt; $ j ++){
if($ row [$ j] =='0'|| $ row [$ j]!= ''){
if($ csv_enclosed ==' ')$ schema_insert。= $ row [$ j];
else {
$ schema_insert。= $ csv_enclosed。
str_replace($ csv_enclosed,$ csv_escaped。$ csv_enclosed,$ row [$ j])。 $ csv_enclosed;
}
} else $ schema_insert。='';
if($ j <$ fields_cnt - 1)$ schema_insert。= $ csv_separator;
}
$ out。= $ schema_insert;
$ out。= $ csv_terminated;
header(Cache-Control:must-revalidate,post-check = 0,pre-check = 0); // - >警告
header(Content-Length:。strlen($ out)); // - >警告
标题(Content-type:text / x-csv); // - >警告
header(Content-Disposition:attachment; filename = somefile.csv); // - >警告
echo $ out;
出口;
但它返回错误:
警告:无法修改标题信息 - 已经发送的标题(输出始于XX行'文件'中的输出
当我直接运行代码而没有任何类和函数时,它可以正常工作。我试着用 var_dump来检查标题列表(headers_list())
和我有:
array(5){
[0] => string(23)X-Powered-By:PHP / 5.4.4
[1] => string(38)Expires:Thu,1981年11月19日08:52: 00 GMT
[2] => string(77)Cache-Control:no-store,no-cache,must-revalidate,post-check = 0,pre-check = 0
(3)=>字符串(16)Pragma:no-cache
[4] =>字符串(23)Content-type:text / html
}
我试着用 header_remove();
但它是一样的。如何解决这个问题?谢谢...
尝试在开始处添加 ob_start()
,并在结尾处添加 ob_flush()
p>
What I want to do is, calling a class for exporting mysql data to the excel from another class. Below is the code I've used (I've omitted the code for opening the connection):
//This is the class for calling the `ExportToExcel` class
Class SampleClass {
public static function mainFunction() {
ExportToExcel::exportToExcel();
}
}
Class ExportToExcel {
public static function exportToExcel() {
$DB = DB::Open(); //Open the database
$csv_terminated = "\n";
$csv_separator = ",";
$csv_enclosed = '"';
$csv_escaped = "\\";
$sql_query = "The Query goes here...";
$result = $DB->qry($sql_query, 2, TRUE); //Run the query
$fields_cnt = 0;
$schema_insert = '';
while ($finfo = mysqli_fetch_field($result)) {
$fields_cnt++;
$l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, stripslashes($finfo->name)) . $csv_enclosed;
$schema_insert .= $l;
$schema_insert .= $csv_separator;
}
$out = trim(substr($schema_insert, 0, -1));
$out .= $csv_terminated;
while ($row = mysqli_fetch_array($result)) {
$schema_insert = '';
for ($j = 0; $j < $fields_cnt; $j++) {
if ($row[$j] == '0' || $row[$j] != '') {
if ($csv_enclosed == '') $schema_insert .= $row[$j];
else {
$schema_insert .= $csv_enclosed .
str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j]) . $csv_enclosed;
}
} else $schema_insert .= '';
if ($j < $fields_cnt - 1) $schema_insert .= $csv_separator;
}
$out .= $schema_insert;
$out .= $csv_terminated;
}
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); //--> WARNING
header("Content-Length: " . strlen($out)); //--> WARNING
header("Content-type: text/x-csv"); //--> WARNING
header("Content-Disposition: attachment; filename=somefile.csv"); //--> WARNING
echo $out;
exit;
}
}
But it returns error:
Warning: Cannot modify header information - headers already sent by (output started at 'files' on line XX
When I run the code directly without any class and function, it works. I've tried to check the header list using var_dump(headers_list())
and I've got:
array(5) {
[0]=> string(23) "X-Powered-By: PHP/5.4.4"
[1]=> string(38) "Expires: Thu, 19 Nov 1981 08:52:00 GMT"
[2]=> string(77) "Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0"
[3]=> string(16) "Pragma: no-cache"
[4]=> string(23) "Content-type: text/html"
}
I've tried to remove the header using header_remove();
but it was the same. How to solve the problem? Thanks...
try to add ob_start()
at start and ob_flush()
at end
这篇关于无法修改静态功能上的标题信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!