mongodb for file storage system

Preface: This deep pit Yeah, to the corresponding version of mongodb with php supported version, and then, if you use the composer install third-party libraries, must be one to one

 

 

 

 

Positive start!

 

Development Environment:

System: window

Development languages: php + apache + tp5

 

一、window下安装mongodb:http://baijiahao.baidu.com/s?id=1601512248926547477&wfr=spider&for=pc

Second, a third party library by downloading composer (package class)

mongosStorage.php

? < PHP
 / * * 
 * PhpStorm the Created by. 
 * The User: GaN 
 * a Date: 2019/10/22 
 * Time: 14:58 
 * / 
namespace App \ the Common \ lib; // namespace 
class mongosStorage { 

    // 1, must define a static private property 
    // 2, defines a private constructor 
    // 3, singleton is to avoid multiple new same object to form a unified intersection 
    private  static  $ obj = null ;
     private  $ Collection = null ;
     private  function the __construct () 
    { 
        $ the this -> = Collection ( new new \ MongoDB \ Client) -> Storage->selectGridFSBucket (); 
    } 

    public  static  function singleEntrance () {
         // determines whether an object is instantiated through 
        IF (Self ::! $ obj the instanceof Self) { 
            Self :: $ obj = new new Self (); // if not necessary examples of what 
        }
         return Self :: $ obj ; 
    } 

    // prevent external clone 
    public  function a __clone () 
    { 
        // the TODO: Implement a __clone () Method. 
    } 

    / * * 
     * @param $ filename (filename only, no path)
     * @Param $ filepath (absolute file path) 
     * / 
    public  function Deposit ( $ filename , $ filepath ) {
         $ Stream = $ the this -> Collection-> openUploadStream ( $ filename );
         $ Contents = file_get_contents ( $ filepath );
         fwrite ( $ Stream , $ Contents );
         fclose ( $ Stream ); 
    } 

    / * * 
     * get the file ID 
     * @param $ filename (filename only, no path) 
     * Mixed @return 
     * / 
    public  function getFileId ( $ filename = '') {
         $ Stream = $ the this -> Collection-> openDownloadStreamByName ( $ filename );
         $ fileId = $ the this -> Collection-> getFileIdForStream ( $ Stream );
         return  $ fileId ; 
    } 


    / * * 
     * get 
     * @param $ filename ( only the file name, no path) 
     * / 
    public  function Take ( $ filename = '' ) {
         $ Stream = $ the this -> Collection-> openDownloadStreamByName ( $ filename , [ 'Revision' => 0 ]);
         return stream_get_contents($stream);
    }

}

 

Second, it is displayed on the html

 

 

 

* Since I deposited, the type of content is not saved, so take the time little trouble *

After removing the binary image, by generating image php, html image display request

 

Image manipulation controller file code

<?php

/**
 * FLY rms 图片操作控制器
 * @author fly
 * 2018-9-30 15:52:21
 */

namespace app\admin\controller;
use app\common\lib\mongosStorage;
use think\Controller;
use think\Request;

class Image extends Admin
{

    
    //上传功能
    public function upload(){
        
        $return = array(
                'msg'=>'fall',
                'code'=>201,
                'mongodb_id'=>'',
                'filename'=>'',
        );
        
        try {


            // Move to the next frame application root / public / uploads / directory 
            $ File = Request () -> File ( 'Image' );
             $ file_name = '' ;
             $ mongodb_id = '' ;
             IF ( $ File ) { 

                $ the root_path __DIR __ = "/../../../ public / uploads / TemporaryFile /." ; 

                // different modules stored in different file 
                IF (! is_dir ( $ the root_path )) {
                    mkdir ( $ the root_path , 0777, to true ); 
                } 
                $ SAVENAME = DATE ( 'YmdHis').rand(10000, 99999);
                $info = $file->move($root_path,$savename);
                if($info){
                    $file_name = $info->getFilename();
                    $obj = mongosStorage::singleEntrance();
                    $obj->deposit($file_name,$root_path.$file_name);
                    $mongodb_id = $obj->getFileId($file_name);
                    unset ( $ info ); // online search is that they are so tp5 
                    @ unlink ( $ root_path . $ file_name ); // delete temporary files 
                } the else {
                     the throw  new new \ Exception ( $ File -> getError ()); 
                } 
            } 
        
            $ return [ 'code'] = 200 is ;
             $ return [ 'MSG'] = "Success" ;
             $ return [ 'mongodb_id'] = $ mongodb_id ;
             $ return['filename'] = $file_name;
        } catch (\Exception $e) {
            $return['msg'] = $e->getMessage();
        }
    
        die(json_encode($return));
    }

    /**
     * 获取图片
     */
    public function getImage(){
        header("Content-Type:image/png");
        $file_name = input('file_name');
        if($file_name){
            $obj = mongosStorage::singleEntrance();
            $file = $obj->take($file_name);
            echo $file;
        }
    }

}

Template file code

  <div class="layui-form-item">
      <img src="{:url('/admin/Image/getImage',['file_name'=>'2019102416593634834.jpg'])}"/>
  </div>

Third, the results show

 

 

 

Because it is a test, so there is nothing to do style process, however, the self-test is feasible

 

URL can be used to:

 mongodb download site next window: https://www.mongodb.org/dl/win32/x86_64-2008plus-ssl?_ga=2.30821752.638885743.1535763516-1240051369.1535763516

mongo document: https://docs.mongodb.com/ecosystem/drivers/php/

composer install third-party libraries Description: https://github.com/mongodb/mongo-php-library/releases

Guess you like

Origin www.cnblogs.com/FLy-1992/p/11737208.html