【问题标题】:upload multi ple images in codeigniter as well as file data in ddata base在codeigniter中上传多个图像以及数据库中的文件数据
【发布时间】:2014-12-12 11:13:05
【问题描述】:

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

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);
        }
        }

【问题讨论】:

    标签: codeigniter


    【解决方案1】:

    这样试试

    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);
            }
    
    }
    

    【讨论】:

    • 谢谢先生,我想知道我的错误在哪里,以备将来参考
    • 您的所有代码都很好,但您必须在循环中编写插入查询,以便每条记录都可以插入数据库中
    • 您在 y 仅插入最后一个数据的循环之外编写了查询
    猜你喜欢
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-28
    • 2012-09-04
    • 2016-09-05
    • 1970-01-01
    相关资源
    最近更新 更多