代碼:
function downloadFile(){
//文件路徑(路徑+文件名)
$file_name = U('/temp/downloadfile.xlsx','',false,true);
// required
header('Pragma: public');
//no cache
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private',false);
//強制下載
header('Content-Type:application/force-download');
header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
header('Content-Transfer-Encoding: binary');
header('Connection: close');
//輸出到瀏覽器
readfile($file_name);
exit();
}
代碼2:
//文件路徑
$filename = U('/temp/downloadfile.xlsx','',false,true);
header("Content-Type:application/force-download");
header("Content-Disposition:attachment;filename=".basename($filename));
readfile($filename);