【发布时间】:2012-08-30 01:26:16
【问题描述】:
sn-p 读取新文件 CSV 中的输出流。那部分有效。我可以从服务器打开文件,它看起来很完美。问题在于通过浏览器下载到我的硬盘驱动器的文件。它无法在电子表格软件或 Windows 机器上的 Excel 中正确打开和读取:
$NewFile = fopen($FileName,"w");
fwrite($NewFile, $output);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$FileName.'"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($NewFile));
ob_clean();
flush();
readfile($NewFile);
CSV 的内容看起来就像我从浏览器下载打开它时一样,但当我直接在服务器上打开它或使用 FTP 下载存储的文件时看起来很完美。
【问题讨论】:
-
您是否尝试过通过浏览器阅读,而不是尝试下载?这可以告诉您是您的文件被浏览器弄乱了,还是您的下载代码损坏了某些东西。
-
既然是 csv,编码不应该是
Content-Type: text/plain甚至是text/csv -
@AdrianCornish 我相信你是对的。作为一个纯文本文件,不管它的扩展名和程序如何解释它,它只是文本,应该相应地保存。