How to store file and download files from mongodb in php7.1

156 views
Skip to first unread message

naren sugar

unread,
Apr 6, 2017, 7:13:23 PM4/6/17
to mongodb-user
Hello 

im trying to upload files to mongo from php7.1 with mongodb extension as below 

$manager = new MongoDB\Driver\Manager("mongodb://user:pwd@host:port/db");
$bucket = new MongoDB\GridFS\Bucket($manager, 'db');
 
$input = fopen("Home.png", 'rb');
$bucket->uploadFromStream('example', $input, ['chunkSizeBytes' => 14680064]);
echo "<pre>";
print_r($bucket);

im getting an error:: MongoDB\GridFS\Bucket class not found.. need to include externally this class or any extension required 

thanks
Naren

Wan Bachtiar

unread,
May 4, 2017, 3:51:05 AM5/4/17
to mongodb-user

im trying to upload files to mongo from php7.1

Hi Naren,

You can use MongoDB PHP Library to upload files into GridFS. Make sure you follow the installation steps, afterwards you can try out below code:

<?php
require 'vendor/autoload.php';

$client = new MongoDB\Client("mongodb://user:pwd@host:port");
$bucket = ($client)->databaseName->selectGridFSBucket();

$file = fopen('/path/to/example.png', 'rb');
$bucket->uploadFromStream('example.png', $file);

?>

For more examples see MongoDB PHP Library GridFS Tutorial).

See also the manual for Composer Autoloading.

Regards,

Wan.

Reply all
Reply to author
Forward
0 new messages