文件下载csv在本地主机,但不是在现场 [英] File download for csv works in local host but not when live

查看:123
本文介绍了文件下载csv在本地主机,但不是在现场的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从嵌套数组创建一个csv文件,它工作正常与下载链接到localhost中的csv文件,但在活动主机上,它不会下载。这是我的php文件:

I am creating a csv file from nested array and it works fine with a download link to the csv file in the localhost, but on live host it won't download. This is what is in my php file:

标题声明:

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

输出csv文件的函数:

The Function that outputs the csv file:

/**
* 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);
}

我在本地使用的链接:

<a href=".../csv/csv.php">Download CSV</a>

无法在Live网站上使用。而不是下载它只需要我到csv.php页面,并输出数组在这样的字符串。

Won't work on Live site. Instead of downloading it just takes me to the csv.php page and outputs the array in a string like this.

... ID,Coach One,Coach Two,Work,Cell,Email,...

...ID,"Coach One","Coach Two",Work,Cell,Email,...

推荐答案

尝试将csv硬编码到代码中。将这些行替换为您要查看的数据是否正在传递的数据包含错误字符。

Try hardcoding the csv into the code. Replace these lines with the ones you have to see if your data being passed has bad characters. Maybe specifying the charset will help.

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/

这篇关于文件下载csv在本地主机,但不是在现场的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆