php - 如何使用codeigniter将图像保存到mysql中?

标签 php mysql codeigniter

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

我的 Controller 中有以下代码:

 $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 手册中对此没有任何内容。

最佳答案

您好,请尝试此代码

   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
       }

    }

关于php - 如何使用codeigniter将图像保存到mysql中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33100119/

相关文章:

php - mysql_data_seek 在 ff 和 chrome 中出错,但在 ie 中有效

php - 一个值转换为字符串的数组?

javascript - 更改个人资料图片php

php - 在 Godaddy 上找不到 CodeIgniter 404 页面

php - 如何将购物车中的多个产品作为单独的行插入数据库中

php - 替代 iframe 来提供购买者信息并允许购买者输入商品是否是在其他网站上购买的?

mysql - 我如何在 MYSQL 6.3 工作台中启用 EER 模型?或者如何为我的 "offers"表创建外键?

php - 如何防止通过地址栏进行sql注入(inject)

php - React前端到Laravel和mysql的axios post请求失败

php - CodeIgniter 3 和 session