Using GridFS

This extension supports MongoGridFS via classes under namespace "\yii\mongodb\file". There you will find specific Collection, Query and ActiveRecord classes.

You can upload a file using yii\mongodb\file\Upload:

$document = Yii::$app->mongodb->getFileCollection()->createUpload()
    ->addContent('Part 1')
    ->addContent('Part 2')
    // ...

You can download the file using yii\mongodb\file\Download:


Each row of the file query result contains yii\mongodb\file\Download instance at the key 'file':

use yii\mongodb\file\Query;

$query = new Query();
$rows = $query->from('fs')

foreach ($rows as $row) {
    var_dump($row['file']); // outputs: "object(\yii\mongodb\file\Download)"
    echo $row['file']->toString(); // outputs file content

Using [\yii\mongodb\file\ActiveRecord]] you can manipulate the file using 'file' property:

use yii\mongodb\file\ActiveRecord;

class ImageFile extends ActiveRecord

$record = new ImageFile();
$record->number = 15;
$record->file = '/path/to/some/file.jpg'; // upload local file to GridFS

$record = ImageFile::find()->where(['number' => 15])->one();
var_dump($record->file); // outputs: "object(\yii\mongodb\file\Download)"
echo $row['file']->toString(); // outputs file content

You may as well operate GridFS files via regular PHP stream resource. You will need to register a stream wrapper provided by this extension - yii\mongodb\file\StreamWrapper. This can be done via \yii\mongodb\file\Connection::registerFileStreamWrapper(). Once stream wrapper is registered, you may open a stream resource using following format:


For example:

Yii::$app->mongodb->registerFileStreamWrapper(); // register stream wrapper

// write a file:
$resource = fopen('gridfs://mydatabase.fs?filename=new_file.txt', 'w');
fwrite($resource, 'some content');
// ...

// write file with several fields:
$resource = fopen('gridfs://mydatabase.fs?filename=new_file.txt&number=17&status=active', 'w');
fwrite($resource, 'file number 17 with status "active"');

// read a file:
$resource = fopen('gridfs://mydatabase.fs?filename=my_file.txt', 'r');
$fileContent = stream_get_contents($resource);