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

Neh*_*gui 2 php 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);
Run Code Online (Sandbox Code Playgroud)

谁能告诉我这个问题可能是什么?

Sha*_*rky 5

我猜一下:

您的程序发送headers 之前会输出一些文本(如果不手动进行操作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文件的其余部分。当然那是腐败的。

如果是这样,您不应在s 之前输出任何内容header

  • 来到这里是因为同样的问题,这让我知道出了什么问题。我的代码在生成的文件之后**输出文本。请注意,我使用 `$xmlWriter->save("php://output");` 方法下载文件,我没有将其保存到服务器。 (2认同)