在php-coignatior中的表单submit中上传不同文件夹中的图像和文档


upload image and doc in different folders on form submit in php codeignatior

我有一个表单,有四个文件,如contactemail、contactname、category、comments。到目前为止,我必须在表单中添加图像和文档输入,使用表单我已将数据插入数据库,如何在表单提交的代码签名中上传图像和文档

图像需要上传到图像文件夹,文档需要上传到文档文件夹这是我的控制器

public function form() {
        $this->load->helper('form');
        $data = $this->login_model->form();
//loading views
load-view-header
load view page
load-view-footer
}

这是我的模型函数

  public function form() {
        $contactemail = $this->input->post('contactemail');
        $contactname = $this->input->post('contactname');
        $category = $this->input->post('category');
        $comments = $this->input->post('comments');
        $data = array(
            'contactemail' => $email,
            'contactname' => $name,
            'category' => $category,
            'comments' => $comments
        );
        $this->db->insert('contact', $data);
        return $data;
    }

下面是一个例子。这是很快拼凑起来的,完全没有经过测试。。。往好了说可能会有错误,往坏了说记忆让我失望了,我完全错过了一些东西,但这可能是一个有用的例子。让我知道它是否有帮助?

视图:

<?php echo $error;?>
<?php echo form_open_multipart('upload/do_upload');?>
<!-- your other form fields  for email, name, category, comments can go here-->
<input type="file" name="image" size="20" />
<input type="file" name="doc" size="20" />
<br /><br />
<input type="submit" value="Submit" />
</form>

控制器:

    function form()
    {
            $this->load->helper(array('form', 'url'));
            $config['upload_path'] = './images/';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '2048';
            $config['max_width']  = '1024';
            $config['max_height']  = '768';
            $this->load->library('upload', $config);
            $data['error'] = "";

            //it would be good to be using the form validation class but here is a quick and dirty check for the submit
            if (isset($_POST['submit']))
            {
                if ( ! $this->upload->do_upload('image'))
                {
                        $data['error'] = $this->upload->display_errors();
                }
                else
                {
                        $image_upload_data = $this->upload->data();
                }
                unset($config);
                $config['upload_path'] = './docs/';
                $config['allowed_types'] = 'doc|docx|txt';
                $config['max_size'] = '2048';
                $this->upload->initialize($config);
                if ( ! $this->upload->do_upload('doc'))
                {
                        $data['error'] .= "<br />".$this->upload->display_errors();
                }
                else
                {
                        $doc_upload_data = $this->upload->data();
                }


                if(!empty($data['error']))
                {
                     $this->load->view('form', $data);
                }
                else
                {
                      $contactemail = $this->input->post('contactemail');
                      $contactname = $this->input->post('contactname');
                      $category = $this->input->post('category');
                      $comments = $this->input->post('comments');
                      $doc_file = $doc_upload_data['full_path'];
                      $image_file = $image_upload_data['full_path'];
                      $form_data = array(
                      'contactemail' => $email,
                      'contactname' => $name,
                      'category' => $category,
                      'comments' => $comments,
                      'doc_file' => $doc_file
                  );

                      $image_data['image_name'] = $image_file;
                      $this->login_model->form($form_data,$image_data);
                      $this->load->view('upload_success', $data);
                }
            }
            else
            {
                $this->load->view('form', $data);
            }
}

型号

public function form($form_data,$image_data) {
        $this->db->insert('contact', $image_data);
        $image_data['d_fk']  = $this->db->insert_id();
        $this->db->insert('images', $image_data);
    }