Skip to content

Commit

Permalink
Add examples on gridfs
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Nov 21, 2023
1 parent 9f8eac1 commit 5c6133a
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
66 changes: 66 additions & 0 deletions examples/gridfs-stream.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/**
* This example demonstrates how to use streams with GridFS.
*/

declare(strict_types=1);

namespace MongoDB\Examples;

use MongoDB\Client;
use MongoDB\Driver\Monitoring\CommandFailedEvent;
use MongoDB\Driver\Monitoring\CommandStartedEvent;
use MongoDB\Driver\Monitoring\CommandSubscriber;
use MongoDB\Driver\Monitoring\CommandSucceededEvent;

use function fclose;
use function feof;
use function fread;
use function fwrite;
use function getenv;
use function range;
use function var_dump;

use const PHP_EOL;

require __DIR__ . '/../vendor/autoload.php';

$client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/');
$client->getManager()->addSubscriber(new class implements CommandSubscriber {
public function commandStarted(CommandStartedEvent $event): void
{
var_dump($event->getCommand());

Check failure on line 33 in examples/gridfs-stream.php

View workflow job for this annotation

GitHub Actions / Psalm

ForbiddenCode

examples/gridfs-stream.php:33:9: ForbiddenCode: Unsafe var_dump (see https://psalm.dev/002)
//var_dump(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS));
}

public function commandSucceeded(CommandSucceededEvent $event): void
{
}

public function commandFailed(CommandFailedEvent $event): void
{
}
});


$gridfs = $client->selectDatabase('test')->selectGridFSBucket();

// Open a stream for writing, similar to fopen with mode 'w'
$stream = $gridfs->openUploadStream('hello.txt');

foreach (range(0, 1_000) as $i) {
fwrite($stream, 'Hello world! ' . $i . PHP_EOL);
}

// Last data are flushed to the server when the stream is closed
fclose($stream);

// Open a stream for reading, similar to fopen with mode 'r'
$stream = $gridfs->openDownloadStreamByName('hello.txt');

while (! feof($stream)) {
$data = fread($stream, 1024);
echo "\n\nRead next " . strlen($data) . " bytes\n";

Check failure on line 64 in examples/gridfs-stream.php

View workflow job for this annotation

GitHub Actions / phpcs

Function strlen() should not be referenced via a fallback global name, but via a use statement.
echo $data;
}
41 changes: 41 additions & 0 deletions examples/gridfs-upload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/**
* This example demonstrates how to upload and download files using GridFS.
*/

declare(strict_types=1);

namespace MongoDB\Examples;

use MongoDB\Client;

use function fopen;
use function fwrite;
use function getenv;
use function rewind;
use function stream_get_contents;

use const PHP_EOL;

require __DIR__ . '/../vendor/autoload.php';

$client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/');

$gridfs = $client->selectDatabase('test')->selectGridFSBucket();

// Create an in-memory stream, this can be any stream source like STDIN or php://input for web requests
$stream = fopen('php://temp', 'w+');
fwrite($stream, 'Hello world!');
rewind($stream);

// Upload a file
$id = $gridfs->uploadFromStream('hello.txt', $stream);

Check failure on line 33 in examples/gridfs-upload.php

View workflow job for this annotation

GitHub Actions / Psalm

MixedAssignment

examples/gridfs-upload.php:33:1: MixedAssignment: Unable to determine the type that $id is being assigned to (see https://psalm.dev/032)
echo 'Inserted file with ID: ' . $id . PHP_EOL;

Check failure on line 34 in examples/gridfs-upload.php

View workflow job for this annotation

GitHub Actions / Psalm

MixedOperand

examples/gridfs-upload.php:34:34: MixedOperand: Right operand cannot be mixed (see https://psalm.dev/059)

// Download the file
$stream = $gridfs->openDownloadStreamByName('hello.txt');
echo 'Downloaded file contents: ' . stream_get_contents($stream) . PHP_EOL;

// Delete the file
$gridfs->delete($id);

0 comments on commit 5c6133a

Please sign in to comment.