下载docx文件时出错

V-K*_*V-K 0 php docx

我正在生成docx并从服务器下载.

private static function downloadFile($fileDir)
{
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename=' . basename($fileDir));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($fileDir));
    readfile($fileDir);
}
Run Code Online (Sandbox Code Playgroud)

这是保存功能.如果我从服务器打开临时目录中的文件,它的工作原理.但下载后,我有错误"文件已损坏".我尝试恢复文件,然后恢复所有确定.错误在哪里?

mxm*_*ehl 5

之前readfile($fileDir),尝试运行ob_clean()flush()清理(​​擦除)并刷新输出缓冲区.

PHP的手册中引用flush():

flush()可能无法覆盖Web服务器的缓冲方案,并且它对浏览器中的任何客户端缓冲没有影响.它也不会影响PHP的用户空间输出缓冲机制.这意味着如果使用ob输出缓冲区,则必须同时调用ob_flush()和flush()来刷新ob输出缓冲区.