Jenil Calcuttawala Jenil Calcuttawala - 5 months ago 27
PHP Question

How to retrieve MongoBinData to a string in PHP?

I have successfully stored an image into a MongoDB database, since it is stored as a Base64 type information; I want to retrieve that BinData to a string. How do I do that? Here I am finding my document using input 'Email'.

<?php

$m = new MongoClient();
$db = $m->mydb2->mycol2;

$result = $db->find(array('Email'=>$em));

foreach( $result as $key){
$susername = $key['Email'];
$imagebody = $key['pic'];
}
echo $imagebody;


?>

EDIT:

As Haresh has said

$imagebody = $key['pic']->bin


works perfectly. But it returns me something like Raw data but if I write this

$imagebody = base64_encode($key['pic']->bin);


then it returns me exact Base64 format.

Answer

According to Documentation

To access the contents of a MongoBinData, use the bin field which return the string Mongo Binary Data

So Try this:

$imagebody = $key['pic']->bin

Hope this works for you.