我的功能有问题。 我的函数的目的是强制下载字符串。真正发生的情况是字符串被输出到屏幕而不是下载。
这是我的功能:
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;
}
最佳答案
问题不在于函数。
通过添加 ob_start()到主代码,现在我可以下载我的字符串了。
关于php - 使用 http-header 强制下载字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/838864/