We use PHP7, latest MongoDB PECL package (1.2.2) - Laravel 5.3 - jenssegers/laravel-mongodb 3.1
I want to use GridFS. It's normally available into the MongoDB PECL package but there are no documentation nor working code example.
You can use Bucket
class for upload and download documents to mongodb grid on mongo-php-library 2.2 driver.
//upload file
$bucket = \DB::connection('mongodb')->getMongoDB()->selectGridFSBucket();
$resource = fopen($file_path, "a+");
$file_id = $bucket->uploadFromStream($file_path, $resource);
//download file
$bucket = \DB::connection('mongodb')->getMongoDB()->selectGridFSBucket();
$file_metadata = $bucket->findOne(["_id" => $file_id]);
$path = $file_metadata->filename;
if(!file_exists($path)) {
$downloadStream = $bucket->openDownloadStream($file_id);
$stream = stream_get_contents($downloadStream, -1);
$ifp = fopen($path, "a+");
fwrite($ifp, $stream);
fclose($ifp);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With