PHPMongoDB GridFS file storage method details

  • 2020-06-07 05:30:19
  • OfStack

// Initialize the gridfs
$conn = new Mongo(); // The connection MongoDB
$db = $conn->photos; // Select database 
$grid = $db->getGridFS(); // achieve gridfs object 

gridfs has three ways to store files

The first is direct file storage

$id = $grid- > storeFile("./logo.png");

The second type stores a file in base 2 stream

$data ="./logo.png");
$id = $grid- > storeBytes($data,array("parame"= > 'Additional parameters will be saved with picture 1 '));

The third saves the file $_FILES for direct form submission

$id = $grid->storeUpload('upfile');// The equivalent of 
$id = $grid->storeFile($_FILES[ ' upfile'][ ' tmp_name']);
//-------------- These are the saved images -- Now let's start reading the picture ----------------//
 Return after saving successfully $id = md5 string $logo = $grid->findOne(array('_id'=>$id));
 // In order to _id Gets the file for the index 
header('Content-type: image/png'); 
// Output image header 
echo $logo ->getBytes(); 
// Output data stream ?>

Thank you for reading, I hope to help you, thank you for your support to this site!

Related articles: