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.

MongoDB\GridFS\Bucket::renameByName()

MongoDB\GridFS\Bucket::renameByName()

Sets a new filename value for a GridFS file and any of its revisions.

function renameByName(string $filename, string $newFilename): void
$filename : string
The filename of the files to rename.
$newFilename : string
The new filename value.

MongoDB\GridFS\Exception\FileNotFoundException if no file was found for the selection criteria.

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

<?php $bucket = (new MongoDB\Client)->test->getGridFSBucket(); $stream = fopen('php://temp', 'w+b');fwrite($stream, "foobar");rewind($stream); $bucket->uploadFromStream('a', $stream); $bucket->renameByName('a', 'b'); var_dump(stream_get_contents($bucket->openDownloadStreamByName('b')));

The output would then resemble:

string(6) "foobar"