Skip to content

Commit 4c6eb66

Browse files
authored
PHPLIB-1248 Add examples on GridFS (mongodb#1196)
1 parent ae2eafe commit 4c6eb66

6 files changed

+138
-14
lines changed
File renamed without changes.

Diff for: examples/gridfs_stream.php

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
/**
4+
* This example demonstrates how to use GridFS streams.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace MongoDB\Examples;
10+
11+
use MongoDB\BSON\ObjectId;
12+
use MongoDB\Client;
13+
14+
use function assert;
15+
use function fclose;
16+
use function feof;
17+
use function fread;
18+
use function fwrite;
19+
use function getenv;
20+
use function strlen;
21+
22+
require __DIR__ . '/../vendor/autoload.php';
23+
24+
$client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/');
25+
// Disable MD5 computation for faster uploads, this feature is deprecated
26+
$bucket = $client->test->selectGridFSBucket(['disableMD5' => true]);
27+
28+
// Open a stream for writing, similar to fopen with mode 'w'
29+
$stream = $bucket->openUploadStream('hello.txt');
30+
31+
for ($i = 0; $i < 1_000_000; $i++) {
32+
fwrite($stream, 'Hello line ' . $i . "\n");
33+
}
34+
35+
// Last data are flushed to the server when the stream is closed
36+
fclose($stream);
37+
38+
// Open a stream for reading, similar to fopen with mode 'r'
39+
$stream = $bucket->openDownloadStreamByName('hello.txt');
40+
41+
$size = 0;
42+
while (! feof($stream)) {
43+
$data = fread($stream, 2 ** 10);
44+
$size += strlen($data);
45+
}
46+
47+
echo 'Read a total of ' . $size . ' bytes' . "\n";
48+
49+
// Retrieve the file ID to delete it
50+
$id = $bucket->getFileIdForStream($stream);
51+
assert($id instanceof ObjectId);
52+
$bucket->delete($id);
53+
54+
echo 'Deleted file with ID: ' . $id . "\n";

Diff for: examples/gridfs-stream-wrapper.php renamed to examples/gridfs_stream_wrapper.php

+9-10
Original file line numberDiff line numberDiff line change
@@ -18,42 +18,41 @@
1818
use function getenv;
1919
use function stream_context_create;
2020

21-
use const PHP_EOL;
22-
2321
require __DIR__ . '/../vendor/autoload.php';
2422

2523
$client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/');
26-
$bucket = $client->test->selectGridFSBucket();
24+
// Disable MD5 computation for faster uploads, this feature is deprecated
25+
$bucket = $client->test->selectGridFSBucket(['disableMD5' => true]);
2726
$bucket->drop();
2827

2928
// Register the alias "mybucket" for default bucket of the "test" database
3029
$bucket->registerGlobalStreamWrapperAlias('mybucket');
3130

3231
echo 'File exists: ';
3332
echo file_exists('gridfs://mybucket/hello.txt') ? 'yes' : 'no';
34-
echo PHP_EOL;
33+
echo "\n";
3534

3635
echo 'Writing file';
3736
file_put_contents('gridfs://mybucket/hello.txt', 'Hello, GridFS!');
38-
echo PHP_EOL;
37+
echo "\n";
3938

4039
echo 'File exists: ';
4140
echo file_exists('gridfs://mybucket/hello.txt') ? 'yes' : 'no';
42-
echo PHP_EOL;
41+
echo "\n";
4342

4443
echo 'Reading file: ';
4544
echo file_get_contents('gridfs://mybucket/hello.txt');
46-
echo PHP_EOL;
45+
echo "\n";
4746

4847
echo 'Writing new version of the file';
4948
file_put_contents('gridfs://mybucket/hello.txt', 'Hello, GridFS! (v2)');
50-
echo PHP_EOL;
49+
echo "\n";
5150

5251
echo 'Reading new version of the file: ';
5352
echo file_get_contents('gridfs://mybucket/hello.txt');
54-
echo PHP_EOL;
53+
echo "\n";
5554

5655
echo 'Reading previous version of the file: ';
5756
$context = stream_context_create(['gridfs' => ['revision' => -2]]);
5857
echo file_get_contents('gridfs://mybucket/hello.txt', false, $context);
59-
echo PHP_EOL;
58+
echo "\n";

Diff for: examples/gridfs_upload.php

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/**
4+
* This example demonstrates how to upload and download files from a stream with GridFS.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace MongoDB\Examples;
10+
11+
use MongoDB\BSON\ObjectId;
12+
use MongoDB\Client;
13+
14+
use function assert;
15+
use function fclose;
16+
use function fopen;
17+
use function fwrite;
18+
use function getenv;
19+
use function rewind;
20+
use function stream_get_contents;
21+
22+
require __DIR__ . '/../vendor/autoload.php';
23+
24+
$client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/');
25+
26+
// Disable MD5 computation for faster uploads, this feature is deprecated
27+
$gridfs = $client->test->selectGridFSBucket(['disableMD5' => true]);
28+
29+
// Create an in-memory stream, this can be any stream source like STDIN or php://input for web requests
30+
$stream = fopen('php://temp', 'w+');
31+
fwrite($stream, 'Hello world!');
32+
rewind($stream);
33+
34+
// Upload to GridFS from the stream
35+
$id = $gridfs->uploadFromStream('hello.txt', $stream);
36+
assert($id instanceof ObjectId);
37+
echo 'Inserted file with ID: ', $id, "\n";
38+
fclose($stream);
39+
40+
// Download the file and print the contents directly to an in-memory stream, chunk by chunk
41+
$stream = fopen('php://temp', 'w+');
42+
$gridfs->downloadToStreamByName('hello.txt', $stream);
43+
rewind($stream);
44+
echo 'File contents: ', stream_get_contents($stream), "\n";
45+
46+
// Delete the file
47+
$gridfs->delete($id);
48+
49+
echo 'Deleted file with ID: ', $id, "\n";

Diff for: psalm-baseline.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<code><![CDATA[$clientEncryption->decrypt($document->encryptedField)]]></code>
66
</MixedArgument>
77
</file>
8-
<file src="examples/atlas-search.php">
8+
<file src="examples/atlas_search.php">
99
<MixedArgument>
1010
<code><![CDATA[$document['name']]]></code>
1111
<code><![CDATA[$index->name]]></code>

Diff for: tests/ExamplesTest.php

+25-3
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,28 @@ public static function provideExamples(): Generator
9999
'expectedOutput' => $expectedOutput,
100100
];
101101

102+
$expectedOutput = <<<'OUTPUT'
103+
Read a total of 17888890 bytes
104+
Deleted file with ID: %s
105+
OUTPUT;
106+
107+
yield 'gridfs_stream' => [
108+
'file' => __DIR__ . '/../examples/gridfs_stream.php',
109+
'expectedOutput' => $expectedOutput,
110+
];
111+
112+
$expectedOutput = <<<'OUTPUT'
113+
Inserted file with ID: %s
114+
File contents: Hello world!
115+
Deleted file with ID: %s
116+
117+
OUTPUT;
118+
119+
yield 'gridfs_upload' => [
120+
'file' => __DIR__ . '/../examples/gridfs_upload.php',
121+
'expectedOutput' => $expectedOutput,
122+
];
123+
102124
$expectedOutput = <<<'OUTPUT'
103125
File exists: no
104126
Writing file
@@ -109,8 +131,8 @@ public static function provideExamples(): Generator
109131
Reading previous version of the file: Hello, GridFS!
110132
OUTPUT;
111133

112-
yield 'gridfs-stream-wrapper' => [
113-
'file' => __DIR__ . '/../examples/gridfs-stream-wrapper.php',
134+
yield 'gridfs_stream_wrapper' => [
135+
'file' => __DIR__ . '/../examples/gridfs_stream_wrapper.php',
114136
'expectedOutput' => $expectedOutput,
115137
];
116138

@@ -243,7 +265,7 @@ public function testAtlasSearch(): void
243265

244266
OUTPUT;
245267

246-
$this->assertExampleOutput(__DIR__ . '/../examples/atlas-search.php', $expectedOutput);
268+
$this->assertExampleOutput(__DIR__ . '/../examples/atlas_search.php', $expectedOutput);
247269
}
248270

249271
public function testChangeStream(): void

0 commit comments

Comments
 (0)