使用CodeIgniter上传多个文件

时间:2016-12-16 05:31:26

标签: php codeigniter

我正在尝试将两个不同类型的文件发布到我的数据库中。我一直在寻找整个Stack Overvlow的答案,但我仍然找不到它。

这是我的控制者:

$watch

这是我的观点:

public function insertLamaran()
{
 $pas_foto = ($_FILES['pas_foto']['name']);
 $cv = ($_FILES['cv']['name']);
if ($pas_foto !== "")
{
    $config['upload_path'] = './uploads/';
    $config['log_threshold'] = 1;
    $config['allowed_types'] = 'jpg|png|jpeg|gif';
    $config['max_size'] = '100000'; // 0 = no file size limit
    $config['file_name']='smallImage.jpg';
    $config['overwrite'] = false;
    $this->load->library('upload', $config);
    $this->upload->do_upload('pas_foto');
    $upload_data = $this->upload->data();
    $data['pas_foto'] = $upload_data['file_name'];
   }

   if ($cv !== "")
   {
                    $config['file_name']='bigImage.pdf';
                    $config['upload_path'] = './uploads/';
                    $config['allowed_types'] = 'pdf';
                    $config['max_size'] = '100000'; // 0 = no file size limit
                    $config['overwrite'] = false;
                    $this->load->library('upload', $config);
                    $this->upload->do_upload('filename1');
                    $upload_data = $this->upload->data();
                    $data['cv'] = $upload_data['file_name'];
    }

    $data['u_id'] = $this->input->post('u_id');
    $this->M_upload->insertLamaran($data);
}

错误说

  

未定义的索引:pas_foto。文件名:controllers / Upload.php行号:22

有人知道发生了什么,以及我如何解决它?

2 个答案:

答案 0 :(得分:0)

首先,在控制器url中加载constructor帮助器。我认为你的视图文件必须是这样的。需要放enctype="multipart/form-data"进行文件上传。

<form action="<?php echo base_url('upload/insertLamaran');?>" method="post" enctype='multipart/form-data'>
    <input hidden type="text" name="u_id" value="<?php echo ucfirst($this->session->userdata('u_id')); ?>"></input><br>
    <div class="row wowload fadeInLeftBig">
      <div class="col-sm-6 col-sm-offset-3 col-xs-12">
        <label for="pasfoto">Pas Foto (.jpg)</label>
        <input id="pas_foto" type="file" name="pas_foto" multiple="true">
        <label for="CV">CV (.pdf)</label>
        <input id="cv" type="file" name="cv" multiple="true">
        <button class="btn btn-primary" type="submit"><i class="fa fa-paper-plane"></i> Send</button>
      </div>
    </div>
 </form>

答案 1 :(得分:0)

因为我看到你正确的表格类型所以回答pdf文件 你忘了添加pdf文件名

更改此

$this->upload->do_upload('filename1');

进入这个

$this->upload->do_upload('cv');