-
Notifications
You must be signed in to change notification settings - Fork 264
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
/** | ||
* This example demonstrates how to use GridFS streams. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MongoDB\Examples; | ||
|
||
use MongoDB\Client; | ||
|
||
use function fclose; | ||
use function feof; | ||
use function fread; | ||
use function fwrite; | ||
use function getenv; | ||
use function strlen; | ||
|
||
use const PHP_EOL; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
$client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/'); | ||
|
||
$bucket = $client->test->selectGridFSBucket(['disableMD5' => true]); | ||
|
||
// Open a stream for writing, similar to fopen with mode 'w' | ||
$stream = $bucket->openUploadStream('hello.txt'); | ||
|
||
for ($i = 0; $i < 1_000_000; $i++) { | ||
fwrite($stream, 'Hello line ' . $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 = $bucket->openDownloadStreamByName('hello.txt'); | ||
|
||
$size = 0; | ||
while (! feof($stream)) { | ||
$data = fread($stream, 2 ** 10); | ||
$size += strlen($data); | ||
} | ||
|
||
echo 'Read a total of ' . $size . ' bytes' . PHP_EOL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
/** | ||
* This example demonstrates how to upload and download files from a stream with GridFS. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MongoDB\Examples; | ||
|
||
use MongoDB\BSON\ObjectId; | ||
use MongoDB\Client; | ||
|
||
use function assert; | ||
use function fclose; | ||
use function fopen; | ||
use function fwrite; | ||
use function getenv; | ||
use function rewind; | ||
|
||
use const PHP_EOL; | ||
use const STDOUT; | ||
|
||
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 to GridFS from the stream | ||
$id = $gridfs->uploadFromStream('hello.txt', $stream); | ||
assert($id instanceof ObjectId); | ||
echo 'Inserted file with ID: ' . $id . PHP_EOL; | ||
fclose($stream); | ||
|
||
// Download the file and print the contents directly to STDOUT, chunk by chunk | ||
echo 'File contents: '; | ||
$gridfs->downloadToStreamByName('hello.txt', STDOUT); | ||
echo PHP_EOL; | ||
|
||
// Delete the file | ||
$gridfs->delete($id); |