如何在codeigniter中的每个文件上传时将文档类型(名称)及其相应的图像插入数据库中

时间:2015-03-30 11:17:20

标签: php mysql codeigniter

我需要做的是:用户可以从下拉列表中选择文档类型(例如,护照,驾驶执照,选民ID等)以及他想要上传的相应图像。假设他要上传4个文档,那么他应该能够从“文件”输入类型旁边的下拉列表中为每个上传文档选择文档类型:

这是我到目前为止尝试的代码,但它在表中插入Nothing,尽管它可以正常工作,只需一个下拉菜单和多个文件上传

在我的控制器中:

    $this->customer_model->upload_doc();        
    redirect('/customer/index', 'location', 301);

在模特:

    public function upload_doc()
{
    if(isset($_FILES['userfile']))
    {
        $name_array = array();
        $count = count($_FILES['userfile']['size']);
        foreach($_FILES as $key=>$value)
        for($s=0; $s<=$count-1; $s++) 
        {
            $_FILES['userfile']['name'] = $value['name'][$s];
            $_FILES['userfile']['type'] = $value['type'][$s];
            $_FILES['userfile']['tmp_name'] = $value['tmp_name'][$s];
            $_FILES['userfile']['error'] = $value['error'][$s];
            $_FILES['userfile']['size'] = $value['size'][$s];   
            $config['upload_path'] = './assets/document/';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '';
            $config['max_width'] = '';
            $config['max_height'] = '';
            $this->load->library('upload', $config);
            $this->upload->do_upload();
            $data = $this->upload->data();

            $name_array[] = $data['file_name'];
        }           
            //$names= implode(',', $name_array);

            foreach($name_array as $name)
            {
                $db_data = array(
                        'documentType' => $this->input->post('idproof'),
                        'image' => $name,

                );          
                $this->db->insert('document',$db_data);
            }
    //print_r($db_data);
        //die();
    //die();
        //print_r($names);
    }
}   

在视图中:

 Id Proof<?php echo form_dropdown('idProof1', $document,'', 'class= "form-control"');  ?><input type="file" name="userfile[]" id="userfile" size="20" />
<?php echo form_dropdown('idProof2', $document,'', 'class= "form-control"');  ?>    <input type="file" name="userfile[]" id="userfile" size="20" />
<?php echo form_dropdown('idProof3', $document,'', 'class= "form-control"');  ?>    <input type="file" name="userfile[]" id="userfile" size="20" />

 <button class="btn btn-white" type="submit">Cancel</button>
 <button class="btn btn-primary" type="submit">Save changes</button>

如果有人有想法,请指导我在哪里做错了。 谢谢

1 个答案:

答案 0 :(得分:0)

我是在以下代码的帮助下完成的:

在视图中我确实将name属性设为数组<?php echo form_dropdown('idProof[]', $document,'', 'class= "form-control"'); ?>

在模型中我修改了这样的函数:

    public function upload_doc()
{
    if(isset($_FILES['userfile']))
    {
        $name_array = array();
        $count = count($_FILES['userfile']['size']);
        $time=time();
        $id = $this->db->insert_id();
        $doc=$this->input->post('idProof');
        $j = 0;
        foreach($_FILES as $key=>$value)
        for($s=0; $s<=$count-1; $s++) 
        {
            $_FILES['userfile']['name'] = $id."_".$doc[$j]."_".$time."_".$value['name'][$s];
            $_FILES['userfile']['type'] = $value['type'][$s];
            $_FILES['userfile']['tmp_name'] = $value['tmp_name'][$s];
            $_FILES['userfile']['error'] = $value['error'][$s];
            $_FILES['userfile']['size'] = $value['size'][$s];   
            $config['upload_path'] = './userData/';
            $config['allowed_types'] = 'gif|jpg|png|doc|docx|pdf';
            $config['max_size'] = '';
            $config['max_width'] = '';
            $config['max_height'] = '';
            $this->load->library('upload', $config);
            $this->upload->do_upload();
            $data = $this->upload->data();

            $name_array[] = $data['file_name'];
            $j++;
        }           
            //$names= implode(',', $name_array);


            //$doc=$this->input->post('idProof');
            //die();
            $documentType[] = $this->input->post('idProof');
            $keyIndex=0;
            foreach($name_array as $name)
            {


                $db_data = array(
                        'documentType' => $doc[$keyIndex],
                        'image' => $name,
                        'customerId' => $id
                );          
                $this->db->insert('document',$db_data);
                $keyIndex++;
            }
    //print_r($db_data);
        //die();
    //die();
        //print_r($names);
    }

感谢你们提出的建议让我们开始工作..