我们有一个按钮,当我们点击该按钮时,所有的数据会被放入 CSV 文件并下载。虽然 CSV 文件可以成功创建,但是下载代码却无法正常工作。
$fp = fopen("file\customer-list.csv", "w");
fileName = "file\customer-list.csv";
$filePath = "file\customer-list.csv";
$fsize = filesize("file\customer-list.csv");
if(($_POST['csv_download_list']) == "cm")
{
fwrite($fp, $csv);
fclose($fp);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header("Content-Disposition: attachment; filename=\"$fileName\"");
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Length: ' . filesize($filePath));
ob_clean();
flush();
$file = @fopen($filePath,"rb");
if ($file) {
while(!feof($file)) {
print(fread($file, 1024*8));
flush();
}
@fclose($file);
}
exit;