微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php-将图像上传到Codeigniter中的MySQL数据库Blob

我想将图像上传mysql数据库以存储许多信息.我随附了3(MVC)代码供您参考,请帮助我.

参考:http://forum.codeigniter.com/thread-1205.html

我必须在codeigniter中以blob类型将许多图像上传数据库.我已经写过视图,控制器和模型的所有详细信息都已上传,但仅图像未存储.

还请提供如何在codeigniter中显示图像.

解决方法:

模型中的$this-> input-> post(‘photo’)将无法检索图像信息.因为图像存储在$_FILES中而不是$_POST中.因此,您需要在codeignitor中使用upload library,如下所示.

In Controller:

public function update_profile() {
       $id = $this->session->userdata('id');
       $this->load->model('edit_profile_model');

       $config['upload_path'] = './uploads/';
       $config['allowed_types'] = 'gif|jpg|png';
       $config['max_size']  = '100';
       $config['max_width'] = '1024';
       $config['max_height'] = '768';

       $this->load->library('upload', $config);
       $this->upload->do_upload();//upload the file to the above mentioned path
       $this->edit_profile_model->update_db_user_info($id, $this->upload->data());// pass the uploaded information to the model
   } 

In Model:

public function update_db_user_info($id, $imgdata) {
       $imgdata = file_get_contents($imgdata['full_path']);//get the content of the image using its path
       $data = array(
           'fullname' => $this->input->post('fullname'),
           'address' => $this->input->post('address'),
           'state' => $this->input->post('state'),
           'city' => $this->input->post('city'),
           'pincode' => $this->input->post('pincode'),
           'image' => $imgdata,
       );
       $this->db->where('id', $id);
       $this->db->update('userdetails', $data);
   } 

要检索图像,请在下面的模型中编写一个函数.

public function get_image($id){
       $this->db->where('id', $id);
       $result = $this->db->get('userdetails');
       header("Content-type: image/jpeg");
       echo $result['image'];
}

而且,存储图像并从数据库检索也不是一个好习惯.与其尝试将图像存储在文件夹中,然后将路径存储在数据库中,如下所示.

In Model:

public function update_db_user_info($id, $imgdata) {
       $imgdata = $imgdata['full_path'];// get the path of the image
       $data = array(
           'fullname' => $this->input->post('fullname'),
           'address' => $this->input->post('address'),
           'state' => $this->input->post('state'),
           'city' => $this->input->post('city'),
           'pincode' => $this->input->post('pincode'),
           'image' => $imgdata,// change the type of image from blob to varchar or text
       );
       $this->db->where('id', $id);
       $this->db->update('userdetails', $data);
   } 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐