0

我无法从 MongoDB 正确获取 .docx 文件。我有这个作为代码:

<?php 
$m = new Mongo(//params);

$id = $_GET["FID"];

$patternDOCX = '/.docx$/i';
$patternDOC = '/.doc$/i';
$patternPDF = '/.pdf$/i';
$MIME = "";

$gridFS = $m->db->getGridFS();
$filePointer = $gridFS->findOne(array('_id' => new MongoId($id)));

$nameOriginal = $filePointer->file['filename'];


if(preg_match($patternDOCX, $nameOriginal)){
    $MIME = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; 
} elseif(preg_match($patternDOC, $nameOriginal)){
    $MIME = "application/msword"; 
} elseif (preg_match($patternPDF, $nameOriginal)){
     $MIME = "application/pdf"; 
}

header('Content-type: '.$MIME);
header('Content-Disposition: attachment; filename='.$nameOriginal);

echo $gridFS->findOne(array('_id' => new MongoId($id)))->getBytes();
?> 

当请求的文件是 pdf 时,它可以正常下载并且可读。但是,当请求的文件是 .docx 时,它会作为 docx 文件下载,但是当它打开时,它无法确定编码,并且它提供的任何选项都不能使文档正确显示。我是否缺少某种编码标头?任何帮助表示赞赏。

4

0 回答 0