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

你说的曾经没有我的故事 提交于 2019-12-10 16:28:29

问题


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.


回答1:


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



回答2:


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

Another link for your reference.

Though there is not a sample code provided there, but it would help you if you have worked over Mongo with Laravel.

Hope it would help you



来源:https://stackoverflow.com/questions/41489037/using-gridfs-under-laravel-5-3-with-mongo-php-library-2-2-driver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!