2013-12-11 18 views
-1

我正在使用WAMP服务器,并且想要使用CI在数据库中上传图像。数据库中的图像变量是blob数据类型。我的问题如下: 1)如何存储图像而不是文件名,我应该使用哪些数据类型? 2)如何从数据库中检索图像?使用CodeIgniter在mysql中存储和获取图像

我控制器

public function index() 
    { 

    $this->load->model('contact_model'); 

    if ($this->input->post('upload')) { 

    $data['logo_name']= $this->input->post('name'); 
    $data['logo_image']=$_FILES['userfile']['name']; 


    if($this->contact_model->do_upload()) { 

    echo $this->upload->display_errors(); 

    }else { 

    $this->contact_model->Save_image($data); 

     } 
    } 
     $data['getlogo']=$this->contact_model->getlogo(); 
     $this->load->view('templates/header',$data); 
    } 

我的模型

function do_upload() { 

$this-> gallery_path=realpath(APPPATH.'./uploads'); 

    $config = array( 
     'allowed_types' => 'jpg|png|bmp', 
     'upload_path'=>$this->gallery_path, 
     'max_size'=>2000 
    ); 

    $this->load->library('upload',$config); 

    if ($this->upload->do_upload()) { 

     echo "Upload success!";   
     $image_data=$this->upload->data(); 
    } 
    else 
     { 
      echo "Upload failed!"; 
      echo $this->upload->display_errors(); 
     } 

} 

    function getlogo(){ 

    $this->db->where('logo_id',3); 
    return $this->db->get('logo'); 
} 
function save_image($data){ 


    $q= $this->db->insert('logo', $in); 
    return $q; 
    } 

我看来

<?php foreach ($getlogo->result() as $row){ ?> 
       <a href="#" style="padding-right:1px"> 
       <img src="<?php echo base_url();?>/application/uploads/<?php echo $row->logo_image;}?>" 
       height="200px" width="500px" /></a> 
+1

http://www.techcubetalk.com/2009/01/tutorial-on-how-to-store-images-in-mysql-blob-field/ –

+0

可能重复[CodeIgniter:在数据库中存储图像?](http://stackoverflow.com/questions/2775284/codeigniter-storing-an-image-in-the-database) – zzlalani

回答

1

遵循this保存文件成团块

,并使用

<img src="data:image/jpeg;base64,<?php echo base64_encode($image['file_data'])?>'"/> 

用于显示图像