【问题标题】:How to save image in mysql with codeigniter?如何使用codeigniter将图像保存在mysql中?
【发布时间】:2015-10-13 10:18:51
【问题描述】:

到目前为止,一直在数据库中保存图像的路径,但这一次,我必须将实际图像保存在数据库中。我在表中为此准备了一个字段(longblob)

我的控制器中有这段代码:

 $insertWhat = array(

   'dsrequestFileName1'  => $this->input->post('dsrequestFileName1'),
   'dsrequestFile1'      => isset($_FILES['dsrequestFile1']['name'])? $_FILES['dsrequestFile1']['name'] : '',
   'dsrequestFileName2'  => $this->input->post('dsrequestFileName2'),
   'dsrequestFile2'      => isset($_FILES['dsrequestFile2']['name'])? $_FILES['dsrequestFile2']['name'] : ''
 );

 $now = date('Y-m-d-His');
 $valid_extensions = array('gif', 'jpg', 'jpeg', 'png', 'bmp','');
 if($insertWhat['dsrequestFile1'] !=""){    
     if (!in_array(end(explode('.', strtolower($_FILES['dsrequestFile1']['name']))), $valid_extensions)) {
         $this->session->set_flashdata('wrongtype', '<p class=error>Wrong type of file has been uploaded! Only gif, jpg, bmp and png are allowed.</p>');
         redirect('somepage/somefunctionHere');
     }
     $insertWhat['dsrequestFile1'] = str_replace(' ', '_',$insertWhat['dsrequestFile1']);
     $insertWhat['dsrequestFile1'] = $now.$insertWhat['dsrequestFile1'];
 }

 $config['upload_path'] = 'somefolder/imagesfolder';
 $config['allowed_types'] = 'gif|jpg|jpeg|bmp|png';
 $config['max_size']    = '2048';
 $config['max_width']  = '1024';
 $config['max_height']  = '768';
 $config['file_name'] = $insertWhat['dsrequestFile1'];
 $this->load->library('upload', $config);
 $this->upload->initialize($config);   
 // perform the upload
 $this->upload->do_upload('dsrequestFile1');
 $data = array('upload_data' => $this->upload->data('dsrequestFile1'));
 // code that saves the insert data is irrelevant to the question, I can post it here if needed however.

但是,此代码仅上传图片。我应该在这里更改什么,以便我可以将图像保存在数据库中。如果可以的话,我不想上传图片。

不幸的是,CI 手册中没有任何内容。

【问题讨论】:

标签: php mysql codeigniter


【解决方案1】:

你好,请试试这个代码

   if($this->input->post('update_list'))
    {
      if($this->upload->do_upload('profile_pic'))
      {
            $image_file = array('upload_data' => $this->upload->data());
            $img_name=file_get_contents($image_file); /* This function move to image in your database*/


             $this->your_mode->add_image_table($img_name);// pass image as parameter to your model
       }

    }

【讨论】:

  • 谢谢你,你的回答对我帮助很大。
猜你喜欢
  • 2020-04-17
  • 2013-06-23
  • 1970-01-01
  • 2017-06-19
  • 2016-06-27
  • 2013-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多