正在尝试从我们的数据库中创建带有导出的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页面。但是,我无法将结果存储在文件中。任何帮助都将不胜感激。
发布于 2017-08-08 03:27:29
尝试使用以下内容类型之一:
header('Content-Type: application/vnd.ms-excel');
header('Content-Type: application/octet-stream');发布于 2017-08-08 03:37:58
尝试在文件输出之前发送header("Content-Type: application/force-download");。
发布于 2019-02-28 21:09:48
您所要做的就是删除Content-Disposition头并将Content-Type头设置为纯文本:
header('Content-Type: text; charset=utf-8');
https://stackoverflow.com/questions/45554308
复制相似问题