You can use the class Bucketto upload and download documents to the mongodb grid in the mongo-php-library 2.2 driver.
$bucket = \DB::connection('mongodb')->getMongoDB()->selectGridFSBucket();
$resource = fopen($file_path, "a+");
$file_id = $bucket->uploadFromStream($file_path, $resource);
$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);
}
source
share