-
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
107 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,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()); | ||
//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"; | ||
echo $data; | ||
} |
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,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); | ||
echo 'Inserted file with ID: ' . $id . PHP_EOL; | ||
|
||
// Download the file | ||
$stream = $gridfs->openDownloadStreamByName('hello.txt'); | ||
echo 'Downloaded file contents: ' . stream_get_contents($stream) . PHP_EOL; | ||
|
||
// Delete the file | ||
$gridfs->delete($id); |