我有一段PHP代码,从数据库中选择数据并创建CSV文件。
它的运行很好,但是我该如何在CSV文件被创建完成后强制下载它?
我已经在页面顶部添加了头文件,但它并没有下载文件。
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=calls.csv');
$filename = $SettingsCustomer["company"].'-CallsTariff'.'-'.date("YmdHis");
// create a file pointer connected to the output stream
$output = fopen($_SERVER["DOCUMENT_ROOT"].'/file_dump/price_tariffs/'.$filename.'.csv', 'w');
// output the column headings
fputcsv($output, array('Number', 'Description', 'Per Minute Cost'));
// loop over the rows, outputting them
$cost=0;
$sql="SELECT * from call_costs where sequence < '50' ";
$rs=mysql_query($sql,$conn);
while($result = mysql_fetch_array($rs)) {
//check for custom costs
$sql2="SELECT * from call_costs_custom where parent = '".$result["sequence"]."' AND customer_seq = '".$SettingsCustomer["sequence"]."' ";
$rs2=mysql_query($sql2,$conn);
if(mysql_num_rows($rs2) > 0) {
$result2=mysql_fetch_array($rs);
$cost = $result2["cost"];
} else {
$cost = $result["retail"];
}
fputcsv($output, array($result["number"], $result["description"], $cost));
}