fputcsv将HTML代码插入csv文件 [英] fputcsv Inserting HTML code into a csv file
问题描述
我使用fputcsv编写csv文件有问题。它将页面html也放入csv文件。
I have problem with writing csv file using fputcsv. Its putting the page html also into the csv file. Whats wrong with my code ?
//Excel header
header("Content-Disposition: attachment; filename=\"Delivery_Reports.csv\";" );
header("Content-type: application/vnd.ms-excel");
$out = fopen("php://output", 'w');
$flag = false;
// $result = mysql_query("SELECT * FROM senderids ") or die('Query failed!');
//$sel="SELECT number as MobileNumber ,snum as Sender , msg as Subject ,crdate as Date ,status FROM savemsg WHERE userID='".$_SESSION['id']."' ".$str." ORDER BY sn DESC ";
$result = mysql_query("SELECT `count`, `dnd`, `credit`, `sender_id`, `to`, `message`, `status` FROM `reports` WHERE `unq_id` = '$dlr_id'");
while(false !== ($row = mysql_fetch_assoc($result))){
if(!$flag){
$list = array(
"Total"=>"Total",
"DND"=>"DND",
"Credits"=>"Credits",
"From"=>"From",
"To"=>"To",
"Message"=>"Message",
"Status"=>"Status"
);
// display field/column names as first row
fputcsv($out, array_keys($list), ',', '"');
$flag = true;
}
// array_walk($row, 'cleanData');
fputcsv($out, array_values($row), ',', '"');
}
fclose($out);
推荐答案
您不能保证,代码,没有其他输出。如果此代码段之前的代码正在使用输出缓冲,则可以使用 ob_end_clean 。如果此代码段之后的代码导致出现问题,您只需调用 die 即可保持它不运行。但是,如果此代码片段之前的代码正在直接将HTML输出到浏览器,或者代码在输出HTML后绝对必须运行,那么您必须修改该代码才能解决您的问题。
You can't guarantee, from within a snippet of code, that nothing else will be output. If the code before this snippet is using output buffering, you can discard the HTML using ob_end_clean. If the code after this snippet is causing the problem, you can simply call die to keep it from running at all. However, if the code before this snippet is outputting HTML directly to the browser, or the code after it outputs HTML and absolutely has to run, then you'll have to modify that code in order to solve your problem.
正如Tim所提到的,打印 , echo 并输出到伪文件 php:// output
做完全相同的事情。
As Tim mentioned, print, echo and outputting to the pseudo-file php://output
do exactly the same thing.
这篇关于fputcsv将HTML代码插入csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!