使用 php 从浏览器强制下载文件
我已经在点击链接时生成了 csv 文件。但我无法从浏览器下载文件。
这是我的代码。
$filename="Payment_Received.csv";
header( 'Content-Description: File Transfer' );
header( 'Content-Disposition: attachment; filename=' . $filename );
header('Content-Type: text/csv; charset=UTF-8');
print_r($output);
// output is string comma separated if i remove die and then execute
// then csv append html data also, so I keep die
ob_flush();
die();
正在下载 CSV,但它没有内容。
请帮帮我。
I have generated csv file on link click. But I am unable to download file from browser.
Here is my code.
$filename="Payment_Received.csv";
header( 'Content-Description: File Transfer' );
header( 'Content-Disposition: attachment; filename=' . $filename );
header('Content-Type: text/csv; charset=UTF-8');
print_r($output);
// output is string comma separated if i remove die and then execute
// then csv append html data also, so I keep die
ob_flush();
die();
CSV is downloading but for it is with no content.
Please help me out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的脚本输出以下内容:
"1","asd","asds" \n "2","sqda","asds"
,我相信您将很难从中生成.csv那是我的朋友。对于 CSV 输出,请确保您的数据被正确引用和转义,很抱歉将您发送到 php.net,但是 此评论是我能找到的唯一在这个问题上明确提及的例子。该页面上有一些用于 csv 输出的代码示例。祝你好运
If your script outputs this:
"1","asd","asds" \n "2","sqda","asds"
, I believe you will have a hard time generating .csv from that my friend. For CSV output, make sure your data is quoted and escaped properly, I'm sorry to send you to php.net but this comment is the only example I could land that mentions clearly at this issue. There are some code examples on that page for csv output.Good-luck
强制从浏览器下载文件的函数。
请检查代码:
http://w- Shadow.com/blog/2007/08/12/how-to-force-file-download-with-php/
Function to force download a file from browser.
Please check code at:
http://w-shadow.com/blog/2007/08/12/how-to-force-file-download-with-php/