(PECL mongo >=0.9.0)
Examples:

MongoGridFS represents the files and chunks collections. MongoGridFS extends MongoCollection, and an instance of MongoGridFS has access to all of MongoCollection methods, which act on the files collection:

1
2
3
4
5
6
<?php
 
$grid $db->getGridFS();
$grid->update(array("filename" => "foo"), $newObj); // update on the files collection
 
?>

Another example of manipulating metadata:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
 
// save a file
$id $grid->storeFile("game.tgz");
$game $grid->findOne();
 
// add a downloads counter
$game->file['downloads'] = 0;
$grid->save($game->file);
 
// increment the counter
$grid->update(array("_id" => $id), array('$inc' => array("downloads" => 1)));
 
?>

You can also access the chunks collection from an instance of MongoGridFS:

1
2
3
4
5
6
<?php
 
  $chunks $grid->chunks; // $chunks is a normal MongoCollection
$chunks->insert(array("x" => 4));
 
?>

There are some methods for MongoGridFS with the same name as MongoCollection methods, that behave slightly differently. For example, MongoGridFS::remove() will remove any objects that match the criteria from the files collection and their content from the chunks collection.

To store something new in GridFS, there are a couple options. If you have a filename, you can say:

1
2
3
4
5
<?php
 
$grid->storeFile($filenamearray("whatever" => "metadata""you" => "want"));
 
?>

If you have a string of bytes that isn't a file, you can also store that using MongoGridFS::storeBytes():

1
2
3
4
5
<?php
 
$grid->storeBytes($bytesarray("whatever" => "metadata""you" => "want"));
 
?>
MongoGridFS::findOne
  • References/PHP/Function/Database/MongoDB driver/MongoGridFS

(PECL mongo >=0.9.0) Returns a single file matching the criteria

2025-01-10 15:47:30
MongoGridFS::remove
  • References/PHP/Function/Database/MongoDB driver/MongoGridFS

(PECL mongo >=0.9.0) Remove files and their chunks from the database

2025-01-10 15:47:30
MongoGridFS::delete
  • References/PHP/Function/Database/MongoDB driver/MongoGridFS

(PECL mongo >=1.0.8) Remove a file and its chunks from the database

2025-01-10 15:47:30
MongoGridFS::storeFile
  • References/PHP/Function/Database/MongoDB driver/MongoGridFS

(PECL mongo >=0.9.0) Stores a file in the database

2025-01-10 15:47:30
MongoGridFS::storeUpload
  • References/PHP/Function/Database/MongoDB driver/MongoGridFS

(PECL mongo >=0.9.0) Stores an uploaded file in the database

2025-01-10 15:47:30
MongoGridFS::drop
  • References/PHP/Function/Database/MongoDB driver/MongoGridFS

(PECL mongo >=0.9.0) Drops the files and chunks collections

2025-01-10 15:47:30
MongoGridFS::find
  • References/PHP/Function/Database/MongoDB driver/MongoGridFS

(PECL mongo >=0.9.0) Queries for files public

2025-01-10 15:47:30
MongoGridFS::put
  • References/PHP/Function/Database/MongoDB driver/MongoGridFS

(PECL mongo >=1.0.8) Stores a file in the database

2025-01-10 15:47:30
MongoGridFS::__construct
  • References/PHP/Function/Database/MongoDB driver/MongoGridFS

(PECL mongo >=0.9.0) Creates new file collections

2025-01-10 15:47:30
MongoGridFS::get
  • References/PHP/Function/Database/MongoDB driver/MongoGridFS

(PECL mongo >=1.0.8) Retrieve a file from the database

2025-01-10 15:47:30