使用PHPWord下载DOCX文件时获取损坏的文件


Get a corrupted file on download of a DOCX File using PHPWord

我正在尝试使用"PHPWord"下载一个docx文件。

如果我尝试将文件保存到服务器上,它会正常工作。但是如果添加标题下载它,那么文件就会以损坏的形式显示

注意:我使用openOffice打开它。

这是我的代码:

 $document->save($doc);
 header('Content-Description: File Transfer');
 header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
 header("Content-Disposition: attachment; filename=CV.docx");
 header('Content-Transfer-Encoding: binary');
 header('Expires: 0');
 header('Cache-Control: must-revalidate');
 header('Pragma: public');
 header('Content-Length: ' . filesize($doc));
 readfile($doc);

有人能告诉我可能是什么问题吗?

让我猜一猜:

您的程序在发送header之前输出一些文本(如果没有手动echo,则可能是php警告,也算作输出)。因此,在文件的实际输出中,如果用一个简单的文本编辑器将其打开为txt(只需将扩展名重命名为.txt并用记事本打开),第一行将类似于:

Warning: Cannot modify header information - headers already sent by (output started at /some/file.php:12) in /some/file.php on line 23

然后是doc文件的其余部分。当然,这是腐败的。

如果是这种情况,则不应在header的之前输出任何