3

我正在尝试使用“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);

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

4

1 回答 1

6

让我猜一下:

您的程序在发送s之前会输出一些文本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文件的其余部分。当然,这是损坏的。

如果是这种情况,您不应该在s之前输出任何内容header

于 2014-09-17T07:31:36.420 回答