Jenil Calcuttawala Jenil Calcuttawala - 1 year ago 100
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'.


$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;



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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download