PHP MongoDB Gridfs How to store files _mongodb

Source: Internet
Author: User
Tags mongodb php mongodb
<?php
//initialization Gridfs
$conn = new Mongo ();//Connection MongoDB
$db = $conn->photos;//Select database
$grid = $db-> Getgridfs (); Get Gridfs Object
 

Gridfs has three ways of storing files

The first type of direct storage file

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

Second storage file binary stream

$data = Http://www.bkjia.com/PHPjc/get_file_contents ("./logo.png");
$id = $grid->storebytes ($data, Array ("Parame" => ' additional parameters will be deposited with the picture '));

The third type of file to save direct form submissions $_files

$id = $grid->storeupload (' upfile ');//equivalent to
$id = $grid->storefile ($_files[' upfile '] [' tmp_name ']);
--------------above is to save the picture--read the picture below----------------//
Save success return $id = MD5 string $logo = $grid->findone (Array (' _id ' = > $id));
 The
header (' content-type:image/png ') of the file is obtained with _id as index; 
Output image head
echo $logo->getbytes (); 
Output data stream?>

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.