在codeigniter中更新图像

时间:2017-12-01 17:58:45

标签: javascript php twitter-bootstrap codeigniter

我正在使用codeigniter,我想在codeigniter中更新图像。但问题是,当我调用update方法时,图像不存储在数据库中。控制台窗口显示此错误。 (表单包含文件输入,但在表单上缺少method = POST和enctype = multipart / form-data。文件将不会被发送。)帮助我如何解决它。 这是我的代码视图文件:

<form class="form-horizontal"  action="<?php echo site_url('User/update_image')?>" role="form" method="Post" enctype="multipart/form-control">
  <p><input hidden type="text" name="companyid" value="<?php echo $row->pro_id; ?>" /></p>
<div class="profile-userpic">     
<img class="img-responsive img-rounded" src="<?php echo base_url().'image/'.$row->professional_image; ?>" />
</div>
<br>    

<div class="form-group">
<div class="col-sm-8">
<input type="file" name="userfile"/>
</div>
<div class="col-sm-3">
<input type="submit" class="cen" name="submit" value="upload" />
</div>
</div>

</form>

这是我的控制器的代码,我写了更新方法。

public function update_image()
        {
                $config['upload_path']          = './image';
                $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();
                $dataa = array('upload_data'=>  $this->upload->data());

                $professional_info = array(
                'professional_image'=>  $dataa['upload_data']['file_name']);

                $this->UserModel->update_professional_image($professional_info);
                redirect('Professional/professional_profile_personal_info');
        }

以下是我的模特代码:

public function update_professional_image($data)
        {
            $this->db->where('fk_user_id' , $this->session->userdata('user_id'));
            return $this->db->update('professional',$data);
        }

2 个答案:

答案 0 :(得分:0)

好吧,似乎你有enctype = multipart / form-control而不是enctype = multipart / form-data。对于文件上传,使用enctype = multipart / form-data。

切换到相同的位置,它应该开始工作。

答案 1 :(得分:0)

first of all change your enctype="multipart/form-control" to enctype="multipart/form-data"

现在使用此配置。希望它会帮助你

$path = realpath(APPPATH. './image');
$config['upload_path'] = $path;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size']     = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';

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

if (!$this->upload->do_upload('userfile')) 
{
    $error = $this->upload->display_errors();
}
else
{
     $filename = $this->upload->data();
     $professional_info['userfile'] = $filename['file_name'];
     $this->UserModel->update_professional_image($professional_info);
     redirect('Professional/professional_profile_personal_info');
}