skip to Main Content

I want to use MongoDB GridFS via PHP. I am creating a connection following.

$m = new MongoDBDriverManager("mongodb://127.0.0.1:27017")

This code works in a simple insert. How I can use GridFS? The PHP driver needs to change or the existing PHP driver is fine.

I am using the following:

PHP 7.1.33
MongoDB version v3.6.3
MongoDB extension for PHP driver
MongoDB extension version 1.6.1

Code for migration in MySQL to MongoDB

public function documentupload($offset = 0)
{
    // Export MySQL to MongoDB
    $this->autoRender = false;
    
    // MongoDB connection 
    $m = new MongoDBDriverManager("mongodb://127.0.0.1:27017");
    $this->loadModel('Documentupload');
    
    // MySQL query to retrieve data       
    $documents = $this->Documentupload->find('all', array(
        'limit' => 1,
        'offset' => $offset,
    ));
    
    // Loop through export data in MongoDB
    foreach ($documents as $row) {
        echo (int)$row['Documentupload']['id'];
        echo "<br/>";
        $bulk = new MongoDBDriverBulkWrite;

        $document1 = array(
            "id" => (int)$row['Documentupload']['id'],
            "documentfile" => new MongoDBBSONBinary($row['Documentupload']['documentfile'], 
                MongoDBBSONBinary::TYPE_GENERIC)
        );
        $bulk->insert($document1);
        $m->executeBulkWrite('agrilicense.documentupload', $bulk);
    }
}

2

Answers


  1. Chosen as BEST ANSWER

    Finally I have found any solution. I want to share. All PHP extension version is okay.
    Go to github mongodb library here all documentations are available.

    https://github.com/mongodb/mongo-php-library

    Create a blank folder and run

    composer require mongodb/mongodb
    

    If Composer not install first install composer. then put vendor folder in your project. after that include autoload.php file in your file. After included this file mongodb office site tutorial work fine.

    https://docs.mongodb.com/php-library/v1.8/tutorial/

    I am working in cakephp so my code like.

    App::import('Vendor', 'mongo_driver', array('file' => 'mongo_driver'.DS.'autoload.php'));
        
    
    $bucket = (new MongoDBClient)->test->selectGridFSBucket();
    
    $stream = $bucket->openUploadStream('cib_rc.pdf');
    
    $contents = file_get_contents('/home/shiva/Documents/CIB & RC.pdf');
    fwrite($stream, $contents);
    fclose($stream);
    

    Here test is my database name. fs.chunks and fs.file automatic crated through mongodb.


  2. To use GridFS, you’ll need to use Bucket class like this. Reference GridFS

    try {
        $manager = new MongoDBDriverManager('mongodb://localhost:27017');
        $bucket = new MongoDBGridFSBucket($manager, 'databaseName');
    
        $source = '';   // resource Readable Stream
        $metadata = [
            'foo' => 'bar'
        ];
        $result = $bucket->uploadFromStream('example.txt', $source, [
            'metadata' => $metadata
        ]);
        print_r($result);   // Outputs MongoDBBSONObjectId
    } catch (MongoDBDriverExceptionException $e) {
    }
    

    This will create an fs.files document the following abridged data:

    {
        filename: "example.txt"
        metadata: { foo: "bar" }
        // other fields follow...
    }
    

    I’ve tried this locally and it’s working code

    MongoDB Community Server3.6

    mongodb/mongodb php library1.6.1

    MongoDB Driver version (pecl)1.6.1

    MongoDB PHP Extension Module

    /etc/opt/remi/php71/php.d/50-mongodb.ini
    mongodb
    MongoDB support => enabled
    MongoDB extension version => 1.6.1
    MongoDB extension stability => stable
    libmongoc bundled version => 1.15.2
    libmongoc SSL => enabled
    libmongoc SSL library => OpenSSL
    libmongoc crypto => enabled
    libmongoc crypto library => libcrypto
    libmongoc crypto system profile => disabled
    libmongoc SASL => disabled
    libmongoc ICU => disabled
    libmongoc compression => enabled
    libmongoc compression snappy => disabled
    libmongoc compression zlib => enabled
    mongodb.debug => no value => no value
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search