【问题标题】:Image upload to MySQL database blob in codeigniter在 codeigniter 中将图像上传到 MySQL 数据库 blob
【发布时间】:2015-10-29 03:51:56
【问题描述】:

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

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

我必须在 codeigniter 中以 blob 类型将许多图像上传到数据库。我已经编写了视图、控制器和模型,所有细节都在上传,但仅图像不存储。

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

【问题讨论】:

  • 为什么需要将图像存储在数据库中?这通常是一种反模式。此外,将您的代码添加到问题中并明确指出您遇到问题的地方。
  • 那么有什么好的做法..如果一些黑客文件夹所有信息都会去..都是重要信息@MikeBrant
  • 在大多数情况下,应该只将文件路径引用存储到数据库中服务器上文件所在的位置。
  • @MikeBrant 会做,先生,谢谢
  • 由于这个问题只是指向编码论坛中的另一个问题,我认为它不是主题。我们要求这里的问题是自包含的。投票结束。

标签: php mysql codeigniter


【解决方案1】:

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

在控制器中:

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
   } 

在模型中:

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'];
}

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

在模型中:

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);
   } 

【讨论】:

  • Mr.Mahendran 我在 $imgdata['full_path']) 中遇到问题....完整路径意味着请分享该示例
猜你喜欢
  • 2016-05-26
  • 1970-01-01
  • 2016-09-05
  • 1970-01-01
  • 2018-05-09
  • 2013-06-28
  • 2015-04-15
  • 2012-07-23
  • 1970-01-01
相关资源
最近更新 更多