使用php从服务器下载后,docx文件已损坏

时间:2018-05-17 16:15:11

标签: php download docx readfile

我正在尝试使用php从服务器下载word文档(.docx)。不幸的是,我收到的文件已损坏。我可以用文字打开文档,但是我收到这些恼人的消息(文件已损坏等)。这是我的代码:

$file = "documents/".$_POST["id_form"]."_document.docx";
$filename = $_POST["id_form"]."_document.docx";
header("Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document");
header("Content-Disposition: attachment; filename=".$filename);

readfile($file);

感谢您的帮助!

更新解决方案

我得到了解决方案。我必须把 ob_end_clean() ;在标题之前和 退出; 在readfile($ file)之后。现在它工作正常。

以下是工作代码:

    $file = "documents/".$_POST["id_form"]."_document.docx";
    $filename = $_POST["id_form"]."_document.docx";                 
    ob_end_clean();
    header("Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document");
    header("Content-Disposition: attachment; filename=".$filename);
    readfile($file);
    exit;

0 个答案:

没有答案
相关问题