Search code examples
phpmongodbfile-typegridfs

PHP MongoDB extract File Type


I've been working on GridFS with MongoDB + PHP and able to perform CRUD operations. I need to fetch the file extension types being uploaded. For example see below type that is being saved in Mongo for different file types:

image = image/jpeg or image/png
text  = text/plain
docx  = application/octet-stream
xlsx  = application/octet-stream
pdf   = application/pdf

How can I fetch or convert the above mentioned file types like below

image = jpeg or png
text  = txt
docx  = doc
xlsx  = xlsx
pdf   = pdf

Is there any function available to achieve the same ?

Also when any PDF or image of bigger size is uploaded, it inserts more than one records in DB in following form

{
    "_id" : ObjectId("542118ab920662000a000055"),
    "filename" : "blob",
    "type" : "application/octet-stream",
    "title" : "blob",
    "isFolder" : 0,
    "uploadDate" : ISODate("2014-09-23T06:52:27.000Z"),
    "length" : 338277,
    "chunkSize" : 261120,
    "md5" : "d4f9269491c30a0ab086b3bab02c81ee"
}

Please Help.

Thanks.


Solution

  • You could simply add the needed data on save to the GridFS's entry's metadata and query for it as you would do with every other collection, eg.

    db.collection.files.find({"metadata.mimetype":/^image/i})
    

    I don't get the point of why multiple records need to be saved, though. GridFS has two collections by default, one which is called files and the other which is called chunks. The latter will hold the file, regardless of it's size, split into - you guessed it - chunks of 255kB. All this is done automatically when using GridFS.