【发布时间】:2012-08-17 05:45:27
【问题描述】:
虽然我在这里找到了答案: Serving .docx files through Php 但是当我尝试通过 php 下载和打开 docx 服务器时,我仍然收到文件损坏的错误 也许您会发现我的代码有问题。 .doc 工作正常,但失败的是 docx。
$parts = pathinfo($doc);
$docFile = $userDocRoot.$doc;
if ( !file_exists($docFile) ){
throw new Exception("Can not find ".$parts ['basename']." on server");
}
if ( $parts['extension'] == 'docx' ){
header('Content-type: application/vnd.openxmlformats- officedocument.wordprocessingml.document');
header('Content-Disposition: attachment; filename="'.$parts['basename'].'"');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
ob_clean();
flush();
readfile($docFile);
}else{
header('Content-type: application/msword');
header('Content-Disposition: attachment; filename="'.$parts['basename'].'"');
readfile($docFile);
}
【问题讨论】:
-
$docFile的内容是真实的Word文档,还是html标记等其他格式?
-
是的,我实际上创建了一个doc文件并将其保存为docx并上传到服务器
-
您在 Content-Type 中有一个空格。这是这里的错字还是您的代码中的错字?
-
不只是剪切/粘贴到这个问题
-
您将文件保存为
docx文件,还是将其重命名为docx?
标签: php mime-types