2012-10-23 39 views
0

任何人都可以知道PHP創建的Excel文件沒有DialoBox對我怎麼能強迫下載文件而不顯示對話框(打開/保存)。下面的代碼是我的測試腳本下載創建的Excel文件,但對話框似乎下載一個文件。下載使用標頭內容

$filename ="excelreport.xls"; 
$contents = "testdata1 \t testdata2 \t testdata3 \t \n"; 
header('Content-type: application/ms-excel'); 
header('Content-Disposition: attachment; filename='.$filename); 
echo $contents; 

我想自動下載文件並將其保存在指定的目錄,而不對話框

+1

不可能。這是用戶的瀏覽器選項。 – MrYanDao

回答

2

您可以強制,而不是下載顯示它的,但我認爲這是不可能迫使一般的瀏覽器下載它沒有提示

// fix for IE catching or PHP bug issue 
header("Pragma: public"); 
header("Expires: 0"); // set expiration time 
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
// browser must download file from server instead of cache 

// force download dialog 
header("Content-Type: application/force-download"); 
header("Content-Type: application/octet-stream"); 
header("Content-Type: application/download"); 

來源:php.net

1

不能出於安全原因,做到這一點。