1

我的控制器中有这个功能来下载我存储在我的 mongodb 网格中的文件:

function download_presentation($ext,$store_filename)
{               
    $grid = $this->mongo->db->getGridFS();                      
    //query the file object
    $objects = $grid->find();
    //set content-type header, output in browser
    switch ($ext) {
        case 'pdf':
        $mimeType = 'Content-type: application/pdf';
        break;
        case 'jpg':
        $mimeType = 'Content-type: image/jpg';
        break;
        case 'png':
        $mimeType = 'Content-type: image/png';
            break;
        case 'doc':
        $mimeType = 'Content-type: application/msword';
        break;
        case 'docx':
        $mimeType = 'Content-type: application/vnd.openxmlformats-officedocument.wordprocessingml.document';
        break;
        case 'xls':
        $mimeType = 'Content-type: application/vnd.ms-excel';
        break;
        case 'xlsx':
        $mimeType = 'Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
        break;
        case 'ppt':
            $mimeType = 'Content-type: application/x-mspowerpoint';
        break;
            case 'pptx': $mimeType = 'Content-type: application/vnd.openxmlformats-                           officedocument.presentationml.presentation';
        break;
        default:
        $mimeType='Content-type: application/pdf';
                            }
           while($object = $objects->getNext()) :
           if(($object->file['filename'])==$store_filename){                      
           $content=$object->getBytes();
           header("Content-Length: " . strlen($content)); 
           header($mimeType); 
           echo ($content);}            
           endwhile;                        

} 每当我下载 .ppt 文件时,它都会说 PowerPoint 无法打开此文件,因为它不是 .ppt。对于 pptx 文件,当我在下载后打开它时,当我单击 PowerPoint 中弹出的“修复”时它正在工作。同样的事情发生在 .doc/MS-Word 上。只有 pdf 文件运行良好。谁能告诉我是什么问题?

4

1 回答 1

2

好吧,经过一年的大量阅读、理解和实施 PHP,我能够回答我自己的问题,我已经使用ob_clean()方法修复了错误,它就像一个魅力,希望这对未来的人有所帮助......

        $content= $object->getBytes();
        header("Content-Length: " . strlen($content)); 
        header($mimeType); 
        ob_clean(); 
        echo($content);
于 2014-02-07T17:40:21.020 回答