PHP docx 文件检索导致内容不可读

  • 本文关键字:docx 文件 检索 PHP php docx
  • 更新时间 :
  • 英文 :


我正在尝试通过php提供docx文件。它可以工作,但在打开时出现错误,说文件中有不可读的内容,我信任它吗?

$attachment_location = $_SERVER['DOCUMENT_ROOT']."FILEPATH";
if (file_exists($attachment_location)) {
header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
header("Cache-Control: public"); // needed for internet explorer
header('Content-type: application/vnd.openxmlformats- officedocument.wordprocessingml.document');
header("Content-Transfer-Encoding: Binary");
header("Content-Length:".filesize($attachment_location));
header("Content-Disposition: attachment; filename=FILENAME.docx");
readfile($attachment_location);
die();        
} else {
die("Error: File not found.");
} 

我尝试了一些建议,但找不到解决方案? 有人能看到明显的遗漏吗?

只需在 if 语句的开头添加 ob_clean((; 即可。

ob_clean();

最新更新