-
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.
PHPLIB-1248 Add examples on GridFS (#1196)
- Loading branch information
Showing
6 changed files
with
138 additions
and
14 deletions.
There are no files selected for viewing
File renamed without changes.
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,54 @@ | ||
<?php | ||
|
||
/** | ||
* This example demonstrates how to use GridFS streams. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MongoDB\Examples; | ||
|
||
use MongoDB\BSON\ObjectId; | ||
use MongoDB\Client; | ||
|
||
use function assert; | ||
use function fclose; | ||
use function feof; | ||
use function fread; | ||
use function fwrite; | ||
use function getenv; | ||
use function strlen; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
$client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/'); | ||
// Disable MD5 computation for faster uploads, this feature is deprecated | ||
$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 . "\n"); | ||
} | ||
|
||
// 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' . "\n"; | ||
|
||
// Retrieve the file ID to delete it | ||
$id = $bucket->getFileIdForStream($stream); | ||
assert($id instanceof ObjectId); | ||
$bucket->delete($id); | ||
|
||
echo 'Deleted file with ID: ' . $id . "\n"; |
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
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,49 @@ | ||
<?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 function stream_get_contents; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
$client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/'); | ||
|
||
// Disable MD5 computation for faster uploads, this feature is deprecated | ||
$gridfs = $client->test->selectGridFSBucket(['disableMD5' => true]); | ||
|
||
// 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, "\n"; | ||
fclose($stream); | ||
|
||
// Download the file and print the contents directly to an in-memory stream, chunk by chunk | ||
$stream = fopen('php://temp', 'w+'); | ||
$gridfs->downloadToStreamByName('hello.txt', $stream); | ||
rewind($stream); | ||
echo 'File contents: ', stream_get_contents($stream), "\n"; | ||
|
||
// Delete the file | ||
$gridfs->delete($id); | ||
|
||
echo 'Deleted file with ID: ', $id, "\n"; |
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
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