Docs Menu
Docs Home
/ / /
PHP Library Manual
/ /

MongoDB\GridFS\Bucket::rename()

On this page

  • Definition
  • Parameters
  • Errors/Exceptions
  • Examples
MongoDB\GridFS\Bucket::rename()

Selects a GridFS file by its _id and alters its filename.

function rename($id, string $newFilename): void
$id : mixed
The _id of the file 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->selectGridFSBucket();
$stream = fopen('php://temp', 'w+b');
fwrite($stream, "foobar");
rewind($stream);
$id = $bucket->uploadFromStream('a', $stream);
$bucket->rename($id, 'b');
var_dump(stream_get_contents($bucket->openDownloadStreamByName('b')));

The output would then resemble:

string(6) "foobar"

Back

registerGlobalStreamWrapperAlias()