how can I play the ogg file from mongodb through php

109 Views Asked by At

I get the connection with mongodb in php. I can get the filename from the mongodb and display it in the webpage successfully. however, I cannot get the file recourse. if I get wrong , please remind me. thank you very much. I want to get the original file from the mongodb. I have mongodb php driver. someone can guide me? plz..

<?php
try{
    $connection = new Menter code hereongoClient();
    $db = $connection->FypDatabase;
    $grid = $db->getGridFS();
    $data = $grid->find();
    foreach($data as $obj){
        echo $obj->getFilename().'<br/>';
        echo $obj->getSize();
        echo $obj->getResource(); //it output the Resource id #2 
        echo '<video id="video" autoplay="autoplay" width="640" height="480" preload="metadata" >';
        echo '<source src="'.$data->getResource().'"/>';
        echo '<code>your bowser don\'t  support</code>';
        echo '</video>';
    }
    exit;
    $connection->close();
    } catch (MongoConnectionException $e) {
        die('Error connecting to MongoDB server');
    } catch (MongoException $e) {
        die('Error: ' . $e->getMessage());
    }
?>

I am trying to play the video in video tag. And get the video resource from the mongodb. In my mongodb, I have fs.chunks and fs.files collection. I do some research about the gridfs driver for php, it say,

MongoGridFSFile::getResource — Returns a resource that can be used to read the stored file

getResource() method

1

There are 1 best solutions below

5
On

Maybe a typo : $obj->getResource() instead of $data->getResource()

<?php
try{
$connection=new MongoClient();
$db=$connection->FypDatabase;
$grid=$db->getGridFS();
$data=$grid->find();
   foreach($data as $obj){
    echo $obj->getFilename().'<br/>';
    echo $obj->getSize();
    echo $obj->getResource(); //it output the Resource id #2 
    echo '<video id="video" autoplay="autoplay" width="640" height="480" preload="metadata" >';
    echo '<source src="'.$obj->current().'"/>';
    echo '<code>your bowser don\'t  support</code>';
    echo '</video>';
    }
        exit;
        $connection->close();
} catch (MongoConnectionException $e) {
  die('Error connecting to MongoDB server');
} catch (MongoException $e) {
  die('Error: ' . $e->getMessage());
}
?>