-
Notifications
You must be signed in to change notification settings - Fork 266
PHPLIB-1248 Add examples on GridFS #1196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cfa3b9c
Add examples on gridfs
GromNaN 3f2e3aa
Add tests on gridfs examples
GromNaN 413488c
Remove usages of PHP_EOL
GromNaN eac3eee
Disable MD5 for GridFS examples, this feature is deprecated
GromNaN 73a372b
Disable MD5 on GridFS examples
GromNaN File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
File renamed without changes.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I explicitly disabled MD5, that's what developers should do in their applications.