php 檔案下載問題
PHP code
function download_file($filename){ //below to provide the download if (file_exists($filename)) { $file = fopen($filename); header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename=' . basename($filename)); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize($filename) . ' bytes'); //ob_clean(); //flush(); readfile($filename); fclose($file); exit;}$this->_redirect('xxxxxxx');
以上是網上通用的一段php檔案下載的代碼,我的問題是這樣的,我要實現點擊一個button實現下載同時實現重新整理頁面的內容嗎,也就是說我不希望 exit;我在檔案下載了之後還要繼續執行之後的代碼。也就是說我的下載完成之後$this->_redirect('xxxxxxx');這個語句不執行,請教下有什麼解決方案嗎?
------解決方案--------------------
$file = fopen($filename);
readfile($filename);
fclose($file);
直接readfile就行了。
這個重新導向只能用JS做了, 但onclick只能標記一下變數, 等失去焦點檢查一下變數再說.
C/C++ code
[User:root Time:23:25:51 Path:/home/apache/web]$ cat index.html aaa
------解決方案--------------------
不可以,一個HTTP會話只能傳遞一種類型的資料
況且下載對使用者而言是非同步
------解決方案--------------------
開啟而不是儲存,這是因為檔案類型瀏覽器能夠識別,比如chrome會顯示pdf檔案,而不是儲存。
你需要額外添加一個Header回應標頭:
Content-Disposition: attachment; filename="$檔案名稱"
至於如何添加,你可以讓PHP來讀取輸出這個產生的檔案,並用header函數即可。
或者web伺服器也可以配置對檔案強制下載,不過我不知道怎麼設定