PHP打印到浏览器而不是CSV文件

问题描述 投票:0回答:4

尝试使用我们的数据库导出来创建csv文件。这是我正在使用的代码:

if(isset($_POST["Export"])){

  header('Content-Type: text/csv; charset=utf-8');  
  header('Content-Disposition: attachment; filename=data.csv');  
  $output = fopen("php://output", "w");  
  fputcsv($output, array('ID', 'Name', 'Email', 'Phone'));  
  $query = "SELECT userID, name, email, phone from user ORDER BY userID DESC";  
  $result = mysql_query($query);  
  while($row = mysql_fetch_assoc($result))  
  {  
       fputcsv($output, $row);  
  }  
  fclose($output);  

当我单击导出按钮时,它会在浏览器中加载此PHP页面并显示正确的结果。但是我无法将结果存储在文件中。任何帮助,将不胜感激。

php export-to-csv fputcsv
4个回答
0
投票

尝试在文件输出之前发送header("Content-Type: application/force-download");just。


0
投票

尝试使用以下内容类型之一:

header('Content-Type: application/vnd.ms-excel');
header('Content-Type: application/octet-stream');

0
投票

您所要做的就是删除Content-Disposition标头并将Content-Type标头设置为纯文本:

header('Content-Type: text; charset=utf-8');


-1
投票

尝试删除fclose($output);线。

© www.soinside.com 2019 - 2024. All rights reserved.