2012-03-30 59 views
1

我有一個腳本可以用php下載文件,而且效果很好。但是,當我下載一個.doc文件類型,我在MS Word中打開它,它說,我必須選擇一個編碼和大多數文本的格式如下:在php下載之後無法打開.doc文件

--- L ß{ ß{ Gü ² ÿÿ ÿÿ ÿÿ • Ê Ê ( ( Ð Ð Ð Ä ÿÿÿÿ 」¬ 」¬ 」¬ 」¬ T. èM 」 」- â |W ’W ’W ’W ’W ý÷ ý÷ ý÷ è ---

這是我的頭:

header("Pragma: public"); 
header("Expires: 0"); 
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
header("Cache-Control: public"); 
header("Content-Description: File Transfer"); 
header("Content-Type: $mtype"); 
header("Content-Disposition: attachment; filename=\"$newFile\""); 
header("Content-Transfer-Encoding: binary"); 
header("Content-Length: " . $fsize); 
$mtype = application/msword 

謝謝。

回答

3

您必須添加header('Content-type: application/octet-stream');

不知道你什麼$mtype = application/msword應該做的......

,並更改header("Content-Disposition: attachment; filename=\"$newFile\"");header('Content-Disposition: attachment; filename="'.$newFile'."');

頭後只需使用:

ob_clean(); 
ob_end_flush(); 
readfile($file_path); 
exit; 
+0

我改變像你說的,但它使一樣的東西。並在我的標題後,我得到此代碼: \t \t \t $ file = @fopen($ file_path,「rb」); \t \t \t如果($文件){ \t \t \t而{ \t \t \t \t打印(FREAD($文件,1024 * 8))(FEOF($文件)!); \t \t \t \t flush(); \t \t \t \t如果(CONNECTION_STATUS()!= 0){ \t \t \t \t @fclose($文件); \t \t \t \t die(); \t \t \t \t} \t \t \t} \t \t \t @fclose($文件); \t \t \t} – user1304135 2012-03-30 21:53:43

+0

@ user1304135 - 編輯您的問題,並張貼代碼在那裏... – Michal 2012-03-30 21:55:40

+0

@ user1304135 - 我已經更新了答案,一探究竟...... – Michal 2012-03-30 22:00:00