我正在尝试使用“ 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)
谁能告诉我这个问题可能是什么?
我猜一下:
您的程序在发送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