PHP operates on the detail of the MongoDB GridFS storage file

  • 2020-06-15 07:54:37
  • OfStack


<?php 
// Initialize the gridfs 
$conn = new Mongo(); // The connection MongoDB 
$db = $conn->photos; // Select database  
$grid = $db->getGridFS(); // achieve gridfs object  
//gridfs There are 3 A way to store files  
// The first 1 Kind of direct storage file  
$id = $grid->storeFile("./logo.png"); 
// The first 2 Seed storage file 2 Base flow  
$data = get_file_contents("./logo.png"); 
$id = $grid->storeBytes($data,array("parame"=>' Additional parameters will accompany the picture 1 The deposit ')); 
// The first 3 Save files for direct form submission $_FILES 
$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  
?>


Related articles: