2012-03-30 74 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