csv文件下载工作在本地主机,但不是在现场


File download for csv works in local host but not when live

我正在从嵌套数组创建一个csv文件,它可以很好地与本地主机中的csv文件的下载链接一起工作,但在live主机上它不会下载。这是我的php文件:

声明的头文件:

/**
* Declare headers for CSV
 */
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=registration.csv");
header("Pragma: no-cache");
header("Expires: 0");

输出csv文件的函数:

/**
* Function will output the scsv file
* @param the csv $data in nested array form array(array("","",""),array("",""...)...)
*/
function outputCSV($data) {
$outstream = fopen("php://output", "w");
function __outputCSV(&$vals, $key, $filehandler) {
    fputcsv($filehandler, $vals); // add parameters if you want
}
array_walk($data, "__outputCSV", $outstream);
fclose($outstream);
}
我在local: 中使用的链接
<a href=".../csv/csv.php">Download CSV</a>

不能在Live站点上工作。而不是下载,它只是带我到csv.php页面,并输出数组在一个字符串,像这样。

…ID,"教练"、"教练"两个,工作,电池,电子邮件,…

尝试将csv硬编码到代码中。将这些行替换为您必须查看传递的数据是否具有不良字符的行。也许指定字符集会有帮助。

header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=data.csv');
$output = fopen('php://output', 'w');
fputcsv($output, array('Column 1', 'Column 2', 'Column 3'));

如下所述:

http://code.stephenmorley.org/php/creating-downloadable-csv-files/

我没有设置真正调试您的代码。如果你愿意,你可以试试这个。我知道它有效。

$out = fopen("php://temp", 'r+');
while (($row = $res->fetch(Zend_Db::FETCH_NUM)) != false) {
    fputcsv($out, $row, ',', '"');
}                       
rewind($out);
$csv  = stream_get_contents($out);
header("Content-Type: application/csv;");
header("Content-Disposition: attachment; filename='"foo.csv'"");        
header("Pragma: no-cache");
header("Expires: 0");        
echo $csv;
exit(0); 

根据需要调整循环以迭代结果