How to get the name of userfiles in controller to add to database

不羁的心 提交于 2019-12-24 00:48:30

问题


This is the code so far. I have 2 images that are getting uploaded and added to the desired map. My question is, how do i get both names from the images so I can add just the names to the database together with my other form information.

public function CreateTypesForGamma() {

        $type              = new stdClass();

        $type->TypeID     = $this->input->post('typeID') == 0 ? null : $this->input->post('typeID');
        $type->Titel        = $this->input->post('titel');
        $type->TechnischeFiche      = $this->input->post('technischeFiche');
        $type->GammaID      = $this->input->post('gammaID');
        $type->ExtLink      = $this->input->post('extLink');
        $type->InfoNL       = $this->input->post('infoNL');
        $type->InfoFR       = $this->input->post('infoFR');
        $type->InfoEN       = $this->input->post('infoEN');

        $files = $_FILES;
            $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();
            $fileName = $_FILES['userfile']['name'];
            if ($i == 0)
             $images[] = $fileName;
            }
              $fileName = implode(',',$images);
              $this->gamma_model->upload_image($fileName);
            system.out.println($images);
        if ($type->TypeID == 0) {
            $type->TypeID = $this->type_model->insert($type);
        } else {
            $this->type_model->update($type);
        }
        redirect('gamma/viewAdminGamma');

}
private function set_upload_options()
    { 
      // upload an image options
        $config = array();
        $config['upload_path'] = APPPATH . 'images/types'; //give the path to upload the image in folder
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '5120';
        $config['overwrite'] = FALSE;
        return $config;
    }

回答1:


Change the code to following

 $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();
            $fileName = $_FILES['userfile']['name'];

             $images[] = $fileName;
             $type->TypeFoto=$fileName[0];// this gives name of first            
             image 
             $type->Lastentabel =$fileName[1];
            }
              $fileName = implode(',',$images);
              $this->gamma_model->upload_image($fileName);
            system.out.println($images);
        if ($type->TypeID == 0) {
            $type->TypeID = $this->type_model->insert($type);
        } else {
            $this->type_model->update($type);
        }
        redirect('gamma/viewAdminGamma');

}
private function set_upload_options()
    { 
      // upload an image options
        $config = array();
        $config['upload_path'] = APPPATH . 'images/types'; //give the path to upload the image in folder
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '5120';
        $config['overwrite'] = FALSE;
        return $config;
    }


来源:https://stackoverflow.com/questions/33276610/how-to-get-the-name-of-userfiles-in-controller-to-add-to-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!