在Laravel 5.3下使用Gridfs和mongo-php-library 2.2驱动程序 [英] Using Gridfs under Laravel 5.3 with mongo-php-library 2.2 driver

查看:128
本文介绍了在Laravel 5.3下使用Gridfs和mongo-php-library 2.2驱动程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用最新的MongoDB PECL软件包(1.2.2)PHP7-Laravel 5.3-jenssegers/laravel-mongodb 3.1

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

我想使用GridFS.通常可以在MongoDB PECL软件包中使用它,但是没有文档,也没有可用的代码示例.

I want to use GridFS. It's normally available into the MongoDB PECL package but there are no documentation nor working code example.

推荐答案

您可以使用Bucket类在mongo-php-library 2.2驱动程序上将文档上传和下载到mongodb网格.

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);
}

这篇关于在Laravel 5.3下使用Gridfs和mongo-php-library 2.2驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆