使用 http-header 强制下载字符串
我的功能有问题。 我的函数的目的是强制下载字符串。 真正发生的情况是字符串被输出到屏幕上而不是下载。
这是我的功能:
function arrayToCSV($vectorDados, $cabecalho)
{
$arr = array();
$arr=$vectorDados;
$csv = $cabecalho . "\n";
foreach($arr as $row) {
$csv .=$row[0] . " " . $row[1] . " ". $row[2] . " " .$row[3] . " \n";
}
$filename = "emails_".date("Y-m-d_H-i",time());
header ("Content-Type: application/octet-stream");
header ("Content-disposition: attachment; filename=".$filename.".csv");
print $csv;
}
I'm having a problem with my function.
The purpose of my function is to force a string to be downloaded. What it really happens is that the string is outputted to the screen and not downloaded.
This is my function:
function arrayToCSV($vectorDados, $cabecalho)
{
$arr = array();
$arr=$vectorDados;
$csv = $cabecalho . "\n";
foreach($arr as $row) {
$csv .=$row[0] . " " . $row[1] . " ". $row[2] . " " .$row[3] . " \n";
}
$filename = "emails_".date("Y-m-d_H-i",time());
header ("Content-Type: application/octet-stream");
header ("Content-disposition: attachment; filename=".$filename.".csv");
print $csv;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题不在于功能。
通过将 ob_start() 添加到主代码中,现在我我能够下载我的字符串。
The problem was not with the function.
By adding ob_start() to the main code, now I'm able to download my string.
尝试使用大写 D 的 Content-Disposition
(内容类型可能是 text/csv)
Try Content-Disposition with a capital D.
(and maybe text/csv for the Content-Type)