用于下载Microsoft Word和Excel文件的http标头

JLe*_*ner 10 html php

如果我默认在文件名中命名,我可以成功下载我的微软词.但是,如果我使用$变量来命名它.文件扩展名将是未知的.

样品:

$No = 1; 
$Name = 'John'; 
$Test = 'Science';

//Download header
$document->save($doc);
header('Content-Description: File Transfer');
header('Content-Type: application/msword');
header("Content-Disposition: attachment; filename='$No_$Name_$Test.docx");
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($doc));
ob_clean();
flush();
readfile($doc);
Run Code Online (Sandbox Code Playgroud)

所以,如果我将我的文件名重命名为变量.文件下载没有docx扩展名.有人可以提供建议吗?

谢谢

Kon*_*nin 18

正确的标题是

for Excel(*.xlsx):

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="' . $fileName . '"');
Run Code Online (Sandbox Code Playgroud)

for Word(*.docx):

header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Disposition: attachment;filename="' . $fileName . '"');
Run Code Online (Sandbox Code Playgroud)

  • 因为它基本上是一个 zip 文件 (2认同)

P M*_*P M 5

改变这个

header('Content-Type: application/msword');
Run Code Online (Sandbox Code Playgroud)

至

header('Content-Type: application/octet-stream');
Run Code Online (Sandbox Code Playgroud)

编辑:

并改变

header("Content-Disposition: attachment; filename='$No_$Name_$Test.docx");
Run Code Online (Sandbox Code Playgroud)

至

header("Content-Disposition: attachment; filename=\"{$No}_{$Name}_{$Test}.docx\"");
Run Code Online (Sandbox Code Playgroud)

  • 请更正您的错误更正-.docx文件的mime类型为application / vnd.openxmlformats-officedocument.wordprocessingml.document,.xlsx文件的mime类型为application / vnd.openxmlformats-officedocument.spreadsheetml.sheet (8认同)
  • @Parahat-http://technet.microsoft.com/zh-cn/library/ee309278.aspx或http://blogs.msdn.com/b/vsofficedeveloper/archive/2008/05/08/office-2007-open -xml-mime-types.aspx (4认同)
  • finfo只是查看魔术文件以匹配签名,并且仅识别压缩文件中的PK标题...它无需执行下一步,而是查看实际的压缩内容http://serverfault.com/questions/338087 / making-libmagic-file-detect-docx-files。官方的mime类型如我上面列出的,下载时应在标题中使用 (4认同)