1024programmer Mongodb Operate MongoDB database GridFS under PHP to store files

Operate MongoDB database GridFS under PHP to store files

<?php
//Initialize gridfs
$conn = new Mongo(); //Connect to MongoDB
$db = $conn->photos; //select database
$grid = $db->getGridFS(); // get gridfs object

$grid = $db->getGridFS(‘file’); //Get the file gridfs object

//gridfs has three ways to store files
//The first type of direct storage file
$id = $grid->storeFile(“./logo.png”); $id = $grid->put(‘./logo.png’, array(‘ower’=>’myname’));

//storeFile has the same effect as put, followed by additional parameters, which are saved in the files file

//The second storage file binary stream
$data = get_file_contents(“./logo.png”);
$id = $grid->storeBytes($data,array(“parame”=>’Additional parameters will be stored with the picture’));

//The third way to save the file $_FILES submitted directly by the form
$id = $grid->storeUpload(‘upfile’);
// equivalent to
$id = $grid->storeFile($_FILES[‘upfile’][‘tmp_name’]);

//————–The above is to save the picture–begin to read the picture below—————-

//Return $id = md5 string after saving successfully
$logo = $grid->findOne(array(‘_id’=>$id)); //Use _id as the index to get the file, or directly file name
header(‘Content-type: image/png’); // output image header
echo $logo ->getBytes(); // output data stream

$grid->remove() delete file

$grid->delete() also deletes files, but only the _id of the file can be passed

$grid->drop() clears all data

$grid->find() and findOne() directly search for files and return IDs, find() does not add parameters, and directly returns all file objects

$grid->getFilename() returns the filename

$grid->getSize() returns the file size

?>

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/operate-mongodb-database-gridfs-under-php-to-store-files/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索