Using Gridfs under Laravel 5.3 with the mongo-php-library 2.2 driver

We are using PHP7, the latest PECL package MongoDB (1.2.2) - Laravel 5.3 - jenssegers / laravel-mongodb 3.1

I want to use GridFS. It is usually available in MongoDB's PECL package, but there is no document or working code example.

+4
source share
2 answers

You can use the class Bucketto upload and download documents to the mongodb grid in the 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);
}
+1
source

You can go through the link. This shows how you can use gridFS with Laravel-mongodb, and you might also need to update the mongo db drivers for this.

.

, , Laravel.

, .

0

Source: https://habr.com/ru/post/1665895/


All Articles