retrieving binary data with php driver includes file lenght

266 views
Skip to first unread message

henrik

unread,
Oct 26, 2009, 6:04:27 AM10/26/09
to mongodb-user
hello,

after finding out how to read the binary data from the MongoBinData
class ($obj['fieldname']->bin) i found out that the 4 byte file lenght
is prefixed, so to actually get the real data i have to use "substr
($obj['fieldname']->bin, 4)". is this working as intended? i think it
would be better so separate the file size and data, for example with
"$obj['fieldname']->size" and "$obj['fieldname']->bin".

Eliot

unread,
Oct 27, 2009, 11:00:48 AM10/27/09
to mongodb-user
Can you send a full example?
looking at the tests i don't quite see that.

Kristina Chodorow

unread,
Oct 27, 2009, 1:40:30 PM10/27/09
to mongod...@googlegroups.com
I tried to reproduce what you're seeing with plain MongoBinData and GridFS and couldn't reproduce it with either.  As Eliot said, it would be really helpful to have an example.

henrik

unread,
Oct 27, 2009, 1:44:25 PM10/27/09
to mongodb-user
the binary insert is done in java so here is the insert code:

Mongo mongo = new Mongo("localhost");
DB db = mongo.getDB("mydb");
DBCollection collection = db.getCollection("myfiles");
String filename = "localfile.jpg";
File file = new File(filename);
FileInputStream is = new FileInputStream(file);
byte[] data = new byte[(int) file.length()];
is.read(data);
is.close();
BasicDBObject myfile = new BasicDBObject("filename", filename).append
("data", data);
collection.insert(myfile);

lets assume that the file localfile.jpg has a size of 13824 bytes then
in the mongo shell the output would look like:

{ "_id" : ObjectId("3dec215d9062e04a6e98d700"), "filename" :
"localfile.jpg", "data" : BinData type: 2 len: 13828 }

so there is a 4 byte difference which is the size of one integer.

the php code to read the data looks like this:

$mongo = new Mongo();
$db = $mongo->selectDB('mydb');
$collection = $db->selectCollection('myfiles');
$query = array('_id' => new MongoId('3dec215d9062e04a6e98d700'));
$myfile = $collection->findOne($query);
file_put_contents('binary.tmp', $myfile['data']->bin);
file_put_contents('substr.tmp', substr($myfile['data']->bin, 4));

the file binary.tmp will have a size of 13828 bytes and the file
substr.tmp will have a size of 13824 and binary equals the original
file localfile.jpg

On 27 Okt., 18:40, Kristina Chodorow <krist...@10gen.com> wrote:
> I tried to reproduce what you're seeing with plain MongoBinData and GridFS
> and couldn't reproduce it with either.  As Eliot said, it would be really
> helpful to have an example.
>

Eliot Horowitz

unread,
Oct 27, 2009, 5:09:28 PM10/27/09
to mongod...@googlegroups.com
So the issue is subtypes.
The java driver stores a byte[] as binary subtype 2.
i don't think the PHP driver supports subtypes yet.
Kristina?

Kristina Chodorow

unread,
Oct 28, 2009, 12:51:20 PM10/28/09
to mongod...@googlegroups.com
I didn't notice the difference in the subtype 2 spec.  Just pushed a fix to Github. 
Reply all
Reply to author
Forward
0 new messages