将 csv 保存为附件文件 PHP+Apache 设置不起作用
我有一个有趣的问题,我认为与服务器有关。 我希望当用户单击按钮时将 CSV 数据保存为 CSV 文件。 它在我的开发服务器上运行良好,但在生产中它只是将内容回显到页面。 我的猜测是这一定是服务器问题,但我真的不确定它可能是什么。 GZIP 会影响这个吗?
我的标头代码如下:
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Length: " . strlen($out));
header("Content-type: text/x-csv");
header("Content-Disposition: attachment; filename=$filename");
echo $out;
因此上面的代码适用于开发服务器,而在生产环境中只会将 CSV 回显到页面。 有任何想法吗?
I have a interesting problem that I think is server related. I want CSV data to be saved as a CSV file when a user clicks a button. It works fine on my development server, but on the production it just echo's the content to the page. My guess is that it must be a server issue, but I'm really not sure what it could be. Can GZIP affect this?
My header code is as follows:
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Length: " . strlen($out));
header("Content-type: text/x-csv");
header("Content-Disposition: attachment; filename=$filename");
echo $out;
So this code above works on a development server and on the production will just echo the CSV to the page. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我唯一能想到的就是哑剧类型。 从一台服务器转移到另一台服务器时,我遇到了一些 MIME 类型问题(尤其是 .flv 文件)。
我用于 .csv 文件的是
application/octet-stream
。希望有帮助
The only thing that I can think of is the mime-type. I've had some problems with mime-types (especially with .flv files) when moving from one server to another.
What I've used for .csv files is
application/octet-stream
.Hope that helps
是的,我猜这也是哑剧类型。 您可能想尝试 csv 特定的“text/csv”(RFC 4180) 或通用的“application/octet-stream”。
Yeah I'd guess it's the mime-type as well. You may want to try the csv specific "text/csv" (RFC 4180) or the generic "application/octet-stream".
只需使用 Wireshark (http://www.wireshark.org) 并捕获这两种情况的 HTTP 流量,寻找差异。 Fiddler 也应该工作
Just use Wireshark (http://www.wireshark.org) and capture the HTTP-traffic for both cases and look for differences. Fiddler is also supposed to work