在Laravel 5.3下使用Gridfs和mongo-php-library 2.2驱动程序
作者:互联网
我们使用PHP7,最新的MongoDB PECL包(1.2.2) – Laravel 5.3 – jenssegers / laravel-mongodb 3.1
我想使用GridFS.它通常可用于MongoDB PECL包,但没有文档或工作代码示例.
解决方法:
您可以使用Bucket类在mongo-php-library 2.2驱动程序上上传和下载文件到mongodb网格.
//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);
}
标签:php,laravel-5,gridfs 来源: https://codeday.me/bug/20190706/1392698.html