Multiple image upload with Codeigniter saving only one file path to MySQL Database

前端 未结 2 1212
生来不讨喜
生来不讨喜 2021-01-14 21:29

Now I\'m building an application. I have set up a form that will upload multiple image to database. This is my simple code

View

&l         


        
相关标签:
2条回答
  • 2021-01-14 22:14

    please try this code i hope this is solve

    $files = $_FILES;
            $cpt = count($_FILES['fl']['name']);
            if($cpt<=10)
            {
                $number_of_files = sizeof($_FILES['fl']['tmp_name']);
                $files = $_FILES['fl'];
                $errors = array();
    
                for($i=0;$i<$number_of_files;$i++)
                {
                    if($_FILES['fl']['error'][$i] != 0) $errors[$i][] = 'Couldn\'t upload file '.$_FILES['fl']['name'][$i];
                }
                    if(sizeof($errors)==0)
                    {
                        $this->load->library('upload');
                        $config['upload_path'] = FCPATH . './assets/upload/multiple/';
                        $config['allowed_types'] = 'gif|jpg|png|jpeg';
                        for ($i = 0; $i < $number_of_files; $i++) {
                            //$_FILES['uploadedimage']['ext'] = $this->get_extension($files['name'][$i]);
                            //echo $_FILES['uploadedimage']['ext'];
    
                            $_FILES['uploadedimage']['name'] = time().$i.$files['name'][$i];
                            $_FILES['uploadedimage']['type'] = $files['type'][$i];
                            $_FILES['uploadedimage']['tmp_name'] = $files['tmp_name'][$i];
                            $_FILES['uploadedimage']['error'] = $files['error'][$i];
                            $_FILES['uploadedimage']['size'] = $files['size'][$i];
                        $fileName[] = $_FILES['uploadedimage']['name'];
    
                        $this->upload->initialize($config);
                        if ($this->upload->do_upload('uploadedimage'))
                        {
                            $data['uploads'][$i] = $this->upload->data();
                        }
                        else
                        {
                            $data['upload_errors'][$i] = $this->upload->display_errors();
                    }
                }
            }
        $fname=implode(",",$fileName);
    
    0 讨论(0)
  • 2021-01-14 22:23

    Okay a few minor changes might help

    public function post(){        
        if($this->_validation()===FALSE){ 
            $this->session->set_flashdata('error', 'Ooops, there was an error');
            redirect(base_url("admin/product"));
        }else{
            $files = $_FILES;
            $images = array();
            $cpt = count($_FILES['userfile']['name']);
                for($i=0; $i<$cpt; $i++){
                $_FILES['userfile']['name']= $files['userfile']['name'][$i];
                $_FILES['userfile']['type']= $files['userfile']['type'][$i];
                $_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
                $_FILES['userfile']['error']= $files['userfile']['error'][$i];
                $_FILES['userfile']['size']= $files['userfile']['size'][$i];
                $this->upload->initialize($this->set_upload_options());
                $this->upload->do_upload();
                $images[] = $_FILES['userfile']['name'];
            }
            $fileName = implode(',',$images);
    
            $data = array(  'kodeProduk'                => $this->input->post('kodeproduk'),
                            'ket'                       => $this->input->post('ket'),
    
                            'GambarBesar'               => $fileName
            );
    
            unset($data['submit']);                             
            $this->table->add_record($data);
            $this->session->set_flashdata('success', 'Product has been saved.');
            redirect(base_url("admin/product"));
        }   
    }
    
    0 讨论(0)
提交回复
热议问题