Moodle PHP Documentation 4.1
Moodle 4.1.19+ (Build: 20250711) (5f47dc23ffc)
MongoDB\GridFS\Bucket Class Reference

Public Member Functions

 __construct (Manager $manager, string $databaseName, array $options=[])
 Constructs a GridFS bucket.
 
 __debugInfo ()
 Return internal properties for debugging purposes.
 
 delete ($id)
 Delete a file from the GridFS bucket.
 
 downloadToStream ($id, $destination)
 Writes the contents of a GridFS file to a writable stream.
 
 downloadToStreamByName (string $filename, $destination, array $options=[])
 Writes the contents of a GridFS file, which is selected by name and revision, to a writable stream.
 
 drop ()
 Drops the files and chunks collections associated with this GridFS bucket.
 
 find ($filter=[], array $options=[])
 Finds documents from the GridFS bucket's files collection matching the query.
 
 findOne ($filter=[], array $options=[])
 Finds a single document from the GridFS bucket's files collection matching the query.
 
 getBucketName ()
 Return the bucket name.
 
 getChunksCollection ()
 Return the chunks collection.
 
 getChunkSizeBytes ()
 Return the chunk size in bytes.
 
 getDatabaseName ()
 Return the database name.
 
 getFileDocumentForStream ($stream)
 Gets the file document of the GridFS file associated with a stream.
 
 getFileIdForStream ($stream)
 Gets the file document's ID of the GridFS file associated with a stream.
 
 getFilesCollection ()
 Return the files collection.
 
 getReadConcern ()
 Return the read concern for this GridFS bucket.
 
 getReadPreference ()
 Return the read preference for this GridFS bucket.
 
 getTypeMap ()
 Return the type map for this GridFS bucket.
 
 getWriteConcern ()
 Return the write concern for this GridFS bucket.
 
 openDownloadStream ($id)
 Opens a readable stream for reading a GridFS file.
 
 openDownloadStreamByName (string $filename, array $options=[])
 Opens a readable stream stream to read a GridFS file, which is selected by name and revision.
 
 openUploadStream (string $filename, array $options=[])
 Opens a writable stream for writing a GridFS file.
 
 rename ($id, string $newFilename)
 Renames the GridFS file with the specified ID.
 
 uploadFromStream (string $filename, $source, array $options=[])
 Writes the contents of a readable stream to a GridFS file.
 

Constructor & Destructor Documentation

◆ __construct()

MongoDB\GridFS\Bucket::__construct ( Manager $manager,
string $databaseName,
array $options = [] )

Constructs a GridFS bucket.

Supported options:

  • bucketName (string): The bucket name, which will be used as a prefix for the files and chunks collections. Defaults to "fs".
  • chunkSizeBytes (integer): The chunk size in bytes. Defaults to 261120 (i.e. 255 KiB).
  • disableMD5 (boolean): When true, no MD5 sum will be generated for each stored file. Defaults to "false".
  • readConcern (MongoDB::Driver::ReadConcern): Read concern.
  • readPreference (MongoDB::Driver::ReadPreference): Read preference.
  • typeMap (array): Default type map for cursors and BSON documents.
  • writeConcern (MongoDB::Driver::WriteConcern): Write concern.
Parameters
Manager$managerManager instance from the driver
string$databaseNameDatabase name
array$optionsBucket options
Exceptions
InvalidArgumentExceptionfor parameter/option parsing errors

Member Function Documentation

◆ __debugInfo()

MongoDB\GridFS\Bucket::__debugInfo ( )

Return internal properties for debugging purposes.

See also
https://php.net/manual/en/language.oop5.magic.php#language.oop5.magic.debuginfo
Return values
array

◆ delete()

MongoDB\GridFS\Bucket::delete ( $id)

Delete a file from the GridFS bucket.

If the files collection document is not found, this method will still attempt to delete orphaned chunks.

Parameters
mixed$idFile ID
Exceptions
FileNotFoundExceptionif no file could be selected
DriverRuntimeExceptionfor other driver errors (e.g. connection errors)

◆ downloadToStream()

MongoDB\GridFS\Bucket::downloadToStream ( $id,
$destination )

Writes the contents of a GridFS file to a writable stream.

Parameters
mixed$idFile ID
resource$destinationWritable Stream
Exceptions
FileNotFoundExceptionif no file could be selected
InvalidArgumentExceptionif $destination is not a stream
StreamExceptionif the file could not be uploaded
DriverRuntimeExceptionfor other driver errors (e.g. connection errors)

◆ downloadToStreamByName()

MongoDB\GridFS\Bucket::downloadToStreamByName ( string $filename,
$destination,
array $options = [] )

Writes the contents of a GridFS file, which is selected by name and revision, to a writable stream.

Supported options:

  • revision (integer): Which revision (i.e. documents with the same filename and different uploadDate) of the file to retrieve. Defaults to -1 (i.e. the most recent revision).

Revision numbers are defined as follows:

  • 0 = the original stored file
  • 1 = the first revision
  • 2 = the second revision
  • etc…
  • -2 = the second most recent revision
  • -1 = the most recent revision
Parameters
string$filenameFilename
resource$destinationWritable Stream
array$optionsDownload options
Exceptions
FileNotFoundExceptionif no file could be selected
InvalidArgumentExceptionif $destination is not a stream
StreamExceptionif the file could not be uploaded
DriverRuntimeExceptionfor other driver errors (e.g. connection errors)

◆ drop()

MongoDB\GridFS\Bucket::drop ( )

Drops the files and chunks collections associated with this GridFS bucket.

Exceptions
DriverRuntimeExceptionfor other driver errors (e.g. connection errors)

◆ find()

MongoDB\GridFS\Bucket::find ( $filter = [],
array $options = [] )

Finds documents from the GridFS bucket's files collection matching the query.

See also
Find::__construct() for supported options
Parameters
array | object$filterQuery by which to filter documents
array$optionsAdditional options
Return values
Cursor
Exceptions
UnsupportedExceptionif options are not supported by the selected server
InvalidArgumentExceptionfor parameter/option parsing errors
DriverRuntimeExceptionfor other driver errors (e.g. connection errors)

◆ findOne()

MongoDB\GridFS\Bucket::findOne ( $filter = [],
array $options = [] )

Finds a single document from the GridFS bucket's files collection matching the query.

See also
FindOne::__construct() for supported options
Parameters
array | object$filterQuery by which to filter documents
array$optionsAdditional options
Return values
array|object|null
Exceptions
UnsupportedExceptionif options are not supported by the selected server
InvalidArgumentExceptionfor parameter/option parsing errors
DriverRuntimeExceptionfor other driver errors (e.g. connection errors)

◆ getBucketName()

MongoDB\GridFS\Bucket::getBucketName ( )

Return the bucket name.

Return values
string

◆ getChunksCollection()

MongoDB\GridFS\Bucket::getChunksCollection ( )

Return the chunks collection.

Return values
Collection

◆ getChunkSizeBytes()

MongoDB\GridFS\Bucket::getChunkSizeBytes ( )

Return the chunk size in bytes.

Return values
integer

◆ getDatabaseName()

MongoDB\GridFS\Bucket::getDatabaseName ( )

Return the database name.

Return values
string

◆ getFileDocumentForStream()

MongoDB\GridFS\Bucket::getFileDocumentForStream ( $stream)

Gets the file document of the GridFS file associated with a stream.

Parameters
resource$streamGridFS stream
Return values
array|object
Exceptions
InvalidArgumentExceptionif $stream is not a GridFS stream
DriverRuntimeExceptionfor other driver errors (e.g. connection errors)

◆ getFileIdForStream()

MongoDB\GridFS\Bucket::getFileIdForStream ( $stream)

Gets the file document's ID of the GridFS file associated with a stream.

Parameters
resource$streamGridFS stream
Return values
mixed
Exceptions
CorruptFileExceptionif the file "_id" field does not exist
InvalidArgumentExceptionif $stream is not a GridFS stream
DriverRuntimeExceptionfor other driver errors (e.g. connection errors)

◆ getFilesCollection()

MongoDB\GridFS\Bucket::getFilesCollection ( )

Return the files collection.

Return values
Collection

◆ getReadConcern()

MongoDB\GridFS\Bucket::getReadConcern ( )

Return the read concern for this GridFS bucket.

See also
https://php.net/manual/en/mongodb-driver-readconcern.isdefault.php
Return values
ReadConcern

◆ getReadPreference()

MongoDB\GridFS\Bucket::getReadPreference ( )

Return the read preference for this GridFS bucket.

Return values
ReadPreference

◆ getTypeMap()

MongoDB\GridFS\Bucket::getTypeMap ( )

Return the type map for this GridFS bucket.

Return values
array

◆ getWriteConcern()

MongoDB\GridFS\Bucket::getWriteConcern ( )

Return the write concern for this GridFS bucket.

See also
https://php.net/manual/en/mongodb-driver-writeconcern.isdefault.php
Return values
WriteConcern

◆ openDownloadStream()

MongoDB\GridFS\Bucket::openDownloadStream ( $id)

Opens a readable stream for reading a GridFS file.

Parameters
mixed$idFile ID
Return values
resource
Exceptions
FileNotFoundExceptionif no file could be selected
DriverRuntimeExceptionfor other driver errors (e.g. connection errors)

◆ openDownloadStreamByName()

MongoDB\GridFS\Bucket::openDownloadStreamByName ( string $filename,
array $options = [] )

Opens a readable stream stream to read a GridFS file, which is selected by name and revision.

Supported options:

  • revision (integer): Which revision (i.e. documents with the same filename and different uploadDate) of the file to retrieve. Defaults to -1 (i.e. the most recent revision).

Revision numbers are defined as follows:

  • 0 = the original stored file
  • 1 = the first revision
  • 2 = the second revision
  • etc…
  • -2 = the second most recent revision
  • -1 = the most recent revision
Parameters
string$filenameFilename
array$optionsDownload options
Return values
resource
Exceptions
FileNotFoundExceptionif no file could be selected
DriverRuntimeExceptionfor other driver errors (e.g. connection errors)

◆ openUploadStream()

MongoDB\GridFS\Bucket::openUploadStream ( string $filename,
array $options = [] )

Opens a writable stream for writing a GridFS file.

Supported options:

  • _id (mixed): File document identifier. Defaults to a new ObjectId.
  • chunkSizeBytes (integer): The chunk size in bytes. Defaults to the bucket's chunk size.
  • disableMD5 (boolean): When true, no MD5 sum will be generated for the stored file. Defaults to "false".
  • metadata (document): User data for the "metadata" field of the files collection document.
Parameters
string$filenameFilename
array$optionsUpload options
Return values
resource

◆ rename()

MongoDB\GridFS\Bucket::rename ( $id,
string $newFilename )

Renames the GridFS file with the specified ID.

Parameters
mixed$idFile ID
string$newFilenameNew filename
Exceptions
FileNotFoundExceptionif no file could be selected
DriverRuntimeExceptionfor other driver errors (e.g. connection errors)

◆ uploadFromStream()

MongoDB\GridFS\Bucket::uploadFromStream ( string $filename,
$source,
array $options = [] )

Writes the contents of a readable stream to a GridFS file.

Supported options:

  • _id (mixed): File document identifier. Defaults to a new ObjectId.
  • chunkSizeBytes (integer): The chunk size in bytes. Defaults to the bucket's chunk size.
  • disableMD5 (boolean): When true, no MD5 sum will be generated for the stored file. Defaults to "false".
  • metadata (document): User data for the "metadata" field of the files collection document.
Parameters
string$filenameFilename
resource$sourceReadable stream
array$optionsStream options
Return values
mixedID of the newly created GridFS file
Exceptions
InvalidArgumentExceptionif $source is not a GridFS stream
StreamExceptionif the file could not be uploaded
DriverRuntimeExceptionfor other driver errors (e.g. connection errors)

The documentation for this class was generated from the following file: