无法在GridFs中存储docx,pdf,csv文件

时间:2014-02-28 08:54:22

标签: php mongodb gridfs

我使用Php,MongoDb和GridF来存储和检索文件。它适用于图像,但我想存储带有docx,pdf,csv等扩展名的文件。这是我的代码:

$ext = $this->getFileExt($_FILES["news_attachment"]["name"]);
                switch ($ext) {
                    case 'pdf':
                        $mimeType = 'application/pdf';
                        break;
                    case 'doc':
                        $mimeType = 'application/msword';
                        break;
                    case 'docx':
                        $mimeType = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
                        break;
                    case 'xls':
                        $mimeType = 'application/vnd.ms-excel';
                        break;
                    case 'xlsx':
                        $mimeType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
                        break;
                    case 'ppt':
                        $mimeType = 'application/x-mspowerpoint';
                        break;
                    case 'pptx':
                        $mimeType = 'application/vnd.openxmlformats-officedocument.presentationml.presentation';
                        break;
                    case 'csv':
                        $mimeType = 'text/csv';
                        break;
                    case 'txt':
                        $mimeType = 'text/plain';
                        break;
                    default:
                    $mimeType='application/pdf';
                }
                $gridFs = $this->getGridFs();
                $fileData = array(
                    "filename" => microtime(true) . "." . $ext,
                    "contentType" => $mimeType);
                $_id = $gridFs->storeUpload("news_attachment", $fileData);

但是我得到了同样的错误消息Message: error setting up file:。我已经检查过文件大小为78kb,$fileData也可以。所以,我的问题是 - 还有什么可能导致这个错误?

2 个答案:

答案 0 :(得分:0)

尝试上传时,您是否会提供相同的参数:

$gridFs->storeUpload($_FILES["news_attachment"]["name"], 

或可能

$gridFs->storeUpload($_FILES["news_attachment"]["tmp_name"], 

答案 1 :(得分:0)

我只需要添加错误if(!empty($_FILES["news_attachment"]) && $_FILES["news_attachment"]["error"] == 0){ save file}

的检查