在codeigniter中上传多个图像以及在数据库中上传文件数据

时间:2014-12-12 11:13:05

标签: codeigniter

我也想在codeigniter和更新数据库以及数据库插入文件名,文件路径和标题中上传多个图像,这与所有我上传的图像相同,但在数据库中只能插入一行和文件名是firest,文件路径是最后一个文件。 我的控制器就像这样

function do_upload1() {
        $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']='./uploads/';
                $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();
        $data=$this->upload->data();
        $name_array[]=$data['file_name'];
        }
        $names=implode(',',$name_array);
                    $this->load->database();
                    $db_data = array('id'=> NULL,
                                     'name'=> $names,
                                     'title'     => $this->input->post('title'),
            'file'      => $data['full_path'],
            'width'     => $data['image_width'],
            'height'    => $data['image_height'],
            'type'      => 'uploads/'.$data['file_name'],
            'size'      => $data['file_size'],

            'date'      => time(),




                                     );
                $this->db->insert('upload',$db_data);
        print_r($names);
        }
        }

1 个答案:

答案 0 :(得分:0)

试试这个

function do_upload1() 
{       
        $this->load->database();
        $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']='./uploads/';
            $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();
            $data=$this->upload->data();

        $db_data = array('id'=> NULL,
            'name'=> $names,
            'title'     => $this->input->post('title'),
            'file'      => $data['full_path'],
            'width'     => $data['image_width'],
            'height'    => $data['image_height'],
            'type'      => 'uploads/'.$data['file_name'],
            'size'      => $data['file_size'],
            'date'      => time(),
            );
            $this->db->insert('upload',$db_data);
        }

}