使用codeigniter在数据库中的特定字段存储多个文件

时间:2016-07-14 13:20:46

标签: php mysql file codeigniter upload

我正在创建一个包含20多个字段的表单,以提交员工的详细信息。在表单中,员工必须填写其详细信息并上传其文档。文档有五个字段,即(照片,身份证,许可证,简历,证明),因此每个上载文档的名称必须存储在特定字段中。如果我按顺序上传所有文件,那么它工作正常,但如果员工只插入一个或两个文件,如何将文件名插入到指定的字段。 希望我有意义。

控制器

 public function register()
    {
    //form validation   

    if($this->form_validation->run() === False){
        $this->load->view('form');
    }
    else{

    //uploading files   
    $number_of_files = sizeof(array_filter($_FILES['file']['tmp_name']));
    $files = $_FILES['file'];
    $errors = array();
    for($i=0;$i<$number_of_files;$i++)
    {
        if($_FILES['file']['error'][$i] != 0)
        {
            $error[$i] = array('error' => 'Couldn\'t upload file '.$_FILES['file']['name'][$i]);
            $this->load->view('form',$error);
        }
    }

    //create a new directory for each new user
    $dir = $this->input->post('fname').'_'.$this->input->post('country');
    if( is_dir(FCPATH . 'uploads/'.$dir) === false )
    {
        mkdir(FCPATH . 'uploads/'.$dir);
    }
    $this->load->library('upload');
    $config['upload_path'] = FCPATH . 'uploads/'.$dir;
    $config['allowed_types'] = 'gif|jpg|png|docx';

    for ($i = 0; $i < $number_of_files; $i++) {
    $_FILES['file']['name'] = $files['name'][$i];
    $_FILES['file']['type'] = $files['type'][$i];
    $_FILES['file']['tmp_name'] = $files['tmp_name'][$i];
    $_FILES['file']['error'] = $files['error'][$i];
    $_FILES['file']['size'] = $files['size'][$i];
    $this->upload->initialize($config);

    // we retrieve the number of files that were uploaded
    if ($this->upload->do_upload('file'))
    {
       $data['uploads'][$i] = $this->upload->data();
    }
    else
    {
      $error = array('error' => $this->upload->display_errors());
      $this->load->view('form',$error);
    }
    }
    //assign names of uploaded files to insert in database
    if($data){
        foreach($data as $value){
    $photo   = FCPATH . 'uploads/'.$dir.$value[0]['file_name'];
    $idProof = FCPATH . 'uploads/'.$dir.$value[1]['file_name'];
    $cv      = FCPATH . 'uploads/'.$dir.$value[2]['file_name'];
    $license = FCPATH . 'uploads/'.$dir.$value[3]['file_name'];
    $attest  = FCPATH . 'uploads/'.$dir.$value[4]['file_name'];
    }
    }
     $this->form_model->insert_users($photo,$idProof,$cv,$license,$attest);
    redirect('form/registered');    
    }   
    }

模型

public function insert_users($photo,$idProof,$cv,$license,$attest){

$data = array(
//other fields...
'other'     => $this->input->post('other'),
'photo'     => $photo,
'idProof'   => $idProof,
'cv'        => $cv,
'license'   => $license,
'attest'    => $attest
);
return $this->db->insert('employee',$data);
}

查看

   <input type='file' name='file[]' onchange="readURL(this);" />
   <input type='file' name='file[]' onchange="readURL(this);" />
   <input type='file' name='file[]' onchange="readURL(this);" />
   <input type='file' name='file[]' onchange="readURL(this);" />
   <input type='file' name='file[]' onchange="readURL(this);" />

0 个答案:

没有答案