For AI agents: a documentation index is available at https://www.mongodb.com/zh-cn/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Make the MongoDB docs better! We value your opinion. Share your feedback for a chance to win $100.
MongoDB Branding Shape
Click here >
Docs Menu

MongoDB\GridFS\Bucket::getFileIdForStream()

MongoDB\GridFS\Bucket::getFileIdForStream()

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

function getFileIdForStream(resource $stream): mixed
$stream : resource
The GridFS stream resource.

The _id field of the metadata document associated with the GridFS stream. The return type will depend on the bucket's typeMap option.

MongoDB\GridFS\Exception\CorruptFileException if the file's metadata or chunk documents contain unexpected or invalid data.

MongoDB\Exception\InvalidArgumentException for errors related to the parsing of parameters or options.

MongoDB\Driver\Exception\RuntimeException for other errors at the extension level (e.g. connection errors).

<?php
$bucket = (new MongoDB\Client)->test->selectGridFSBucket();
$stream = $bucket->openUploadStream('filename');
$id = $bucket->getFileIdForStream($stream);
var_dump($id);
fclose($stream);

The output would then resemble:

object(MongoDB\BSON\ObjectId)#3005 (1) {
["oid"]=>
string(24) "5acfb37d7e21e83cdb3e1583"
}