PHP operations on MongoDB gridfs storage files

Source: Internet
Author: User
<? PHP  //  Initialize gridfs  $ Conn = New Mongo (); //  Connect to MongoDB  $ DB = $ Conn -> Photos; //  Select Database  $ Grid = $ DB -> Getgridfs (); // You can retrieve gridfs objects. // There are three ways to store files. // The first method is to store files directly.  $ ID = $ Grid -> Storefile ("./logo.png" );  //  Second, stores the binary stream of a file.  $ Data = Get_file_contents ("./logo.png" );  $ ID = $ Grid -> Storebytes ( $ Data , Array ("Parame" => 'Additional parameters will be stored along with the image ')); //  Third, save the files submitted directly in the form $ _ files  $ ID = $ Grid -> Storeupload ('upfile' );  //  Equivalent  $ ID = $ Grid -> Storefile ( $ _ Files  ['Upfile'] ['tmp _ name']);  //  -------------- Save the image above -- start reading the image below -------------- // After the image is saved successfully, $ id = MD5 string is returned.  $ Logo =$ Grid -> Findone ( Array ('_ Id' => $ ID )); //  Use _ id as the index to retrieve files  Header ('Content-type: image/PNG '); //  Output Image Header  Echo   $ Logo -> Getbytes (); //  Output Data Stream ?>
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.