我正在开发一个允许教师上传文档和学生下载文档的网站。然而,有一个问题。 Microsoft Word (.docx) 文件下载完美,但下载 Excel (xlsx) 文件时,Excel 会显示“此文件已损坏,无法打开”对话框。任何对此的帮助将不胜感激!
我的下载代码如下:
case 'xlsx':
header('Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Pragma: no-cache');
readfile('./uploads/resources/courses/' . $filename);
break;
我遇到了这个问题,并且是BOM https://en.wikipedia.org/wiki/Byte_order_mark.
如何注意它
unzip:使用解压缩检查输出文件,我在第二行看到警告。
$ unzip -l file.xlsx
Archive: file.xlsx
warning file: 3 extra bytes at beginning or within zipfile
...
xxd(十六进制查看器):我使用以下命令看到了前 5 个字节
head -c5 file.xlsx | xxd -g 1
0000000: ef bb bf 50 4b PK...
注意前 3 个字节ef bb bf
那是物料清单!
Why?
也许是带有 BOM 的 php 文件或库的先前输出。
你必须找到带有 BOM 的文件或命令在哪里,就我而言,现在,我没有时间找到它,但我用输出缓冲区解决了这个问题。
<?php
ob_start();
// ... code, includes, etc
ob_get_clean();
// headers ...
readfile($file);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)