使用CodeIgniter的多图像上传器

时间:2018-04-03 15:42:02

标签: image codeigniter file-upload model controller

我是在我的项目中使用代码点火器的新用户,我在上传多个文件时遇到一个问题,但只有function image上传图片并保存到数据库中而function sliderimage没有上传图片或保存到数据库中?

控制器

  public function manage($id = FALSE) {
    $data = array();

    if ($id) {
        $this->{$this->model}->{$this->_primary_key} = $id;
        $data['item'] = $this->{$this->model}->get();
        if (!$data['item'])
            show_404();
    } else {
        $data['item'] = new Std();
    }

    $this->load->library("form_validation");
    $this->form_validation->set_rules("image", 'Image', "trim|callback_image[$id]");
    $this->form_validation->set_rules("sliderimage", 'sliderimage', "trim|callback_image[$id]");

    if ($this->form_validation->run() == FALSE)
        $this->load->view($this->module . '/manage', $data);

    else {
        $this->{$this->model}->save();
        redirect('admin/' . $this->module);
    }
   }

   public function image($var, $id) {
      $config['upload_path'] = './cdn/sliders/';
      $config['allowed_types'] = 'gif|jpg|png|jpeg';
      $this->load->library('upload', $config);
      if ($this->upload->do_upload('image')) {
        $data = $this->upload->data();
        if ($data['file_name'])
            $this->{$this->model}->image = base_url() . '/cdn/sliders/' . $data['file_name'];
    }
    return true;
  }

  public function sliderimage($var, $id) {
    $config['upload_path'] = './cdn/sliders/';
    $config['allowed_types'] = 'gif|jpg|png|jpeg';
    $this->load->library('upload', $config);
    if ($this->upload->do_upload('sliderimage')) {
        $data = $this->upload->data();
        if ($data['file_name'])
            $this->{$this->model}->sliderimage = base_url() . '/cdn/sliders/' . $data['file_name'];
    }
    return true;
  }

Html表格

<form role="form" class="form-horizontal" role="form" 
method="post" enctype="multipart/form-data">
<input class="form-control" type="file" name="image" >
<input class="form-control" type="file" name="sliderimage">
</form>

1 个答案:

答案 0 :(得分:1)

试图同时调用时间图像函数调用:

 $this->form_validation->set_rules("sliderimage", 'sliderimage', "trim|callback_sliderimage[$id]");