D
D
DragDG2016-04-12 22:06:06
PHP
DragDG, 2016-04-12 22:06:06

Rendering images and videos from mongodb with the power of php?

Good afternoon, dear experts!
The crux of the matter is as follows.
There is a task to display data (text, pictures, video) from the database using php.
I am a complete zero php, so the question is:

<?php
  // Config
$dbhost = 'localhost';
$dbname = 'test';

// Connect to test database
$m = new Mongo("mongodb://$dbhost");
$db = $m->$dbname;

// select the collection
$collection = $db->Boroda;

// pull a cursor query
$cursor = $collection->find();
echo $cursor->count() . ' document(s) found. <br/>'; 
foreach ($cursor as $obj) {
echo  $obj['Text0'] . '<br/>';
$count++;
}

$gridFS = $db -> getGridFS();
// Получаем файл по id
$file = $gridFS -> findOne(array('_id' => new MongoId('5706388608baf803e414368d')));
// посылаем пользователю бинарное представление файла
echo $file -> getBytes();
?>

This code outputs text perfectly, but photos and videos are only binary. And it is necessary that files.
Ready to give thanks!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander Kubintsev, 2016-04-12
@akubintsev

Why would you store files in a DB? Keep links to files!

L
lega, 2016-04-12
@lega

You need to specify the correct content-type in the header,
If you need something in the browser to pop up a save file dialog, then you need to specify the content-disposition

A
Alexander Aksentiev, 2016-04-12
@Sanasol

> Binary, only files.
Add a header indicating what you are giving away and that's it.
Insert into the page for example
But this is of course a so-so option.
It is better to make some kind of cache for such a thing, and cache on nginx, for example, what was given.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question