PHP - 使用正确的 MIME 类型打开上传的 DOCX 文件

发布于 2024-08-22 05:16:07 字数 504 浏览 5 评论 0原文

我有用户上传 DOCX 文件,我提供这些文件供下载。我们遇到的问题是 DOCX 文件的未知 mime 类型,导致 IE 将这些文档作为 Zip 文件打开。

它在 Windows/IIS 服务器上运行。

因为这是共享主机,所以我无法更改任何服务器设置。

我想我可以编写一些处理 DOCX 文件的代码,也许是自定义输出:

if (extension=docx) {

header("Content-Disposition: attachment; etc)
header('Content-Type: application/application/vnd.openxmlformats-officedocument.wordprocessingml.document');

//Output the file contents etc

}

这会是一个可行的解决方案吗?如果是这样,有人可以帮助填补空白吗?

(PS我知道上面的语法不正确,只是一个简单的例子)

I have users uploading DOCX files which I make available for download. The issues we have been experiencing is the unknown mime types of DOCX files which causes IE to open these docs as Zip files.

It is running on a Windows/IIS server.

Because this is a shared host, I cannot change any server settings.

I was thinking that I could just write some code that would handle DOCX files, perhaps custom output:

if (extension=docx) {

header("Content-Disposition: attachment; etc)
header('Content-Type: application/application/vnd.openxmlformats-officedocument.wordprocessingml.document');

//Output the file contents etc

}

Would this be a viable solution?? If so, can someone help fill in the gaps?

(PS I know the above syntax is not correct, just a quick example)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

影子的影子 2024-08-29 05:16:07

这应该可以做到:

header('Content-type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Disposition: attachment; filename="myfile.docx"');
readfile('myfile.docx');

This should do it:

header('Content-type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Disposition: attachment; filename="myfile.docx"');
readfile('myfile.docx');
追星践月 2024-08-29 05:16:07

是的,那会很好用。 PHP 文档基本上有您想要的确切的代码

Yes, that will work fine. The PHP docs have basically the exact code you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文