如何使用Codeigniter以1种形式上传PDF和图像文件



我有一份应聘工人的申请表,需要上传一份PDF格式的简历和一份JPG|JPEG|PNG格式的身份证。如何上传这两种不同的文件格式?

My views:

<tr>
<td>Silahkan Upload File CV anda : .PDF</td>
<td>
<input type="file" name="pdf_file">
</td>
<?php if (isset($error)) : ?>
<div class="invalid-feedback"><?= $error ?></div>
<?php endif; ?>
</tr>
<tr>
<td>Silahkan Upload File CV anda : .JPG|JPEG|PNG</td>
<td>
<input type="file" name="ktp_file" id="ktp_file">
</td>
<?php if (isset($error)) : ?>
<div class="invalid-feedback"><?= $error ?></div>
<?php endif; ?>
</tr>

The Controller:

public function store_apply($id){
$lowongan = $this->db->get_where('lowongan', ['id'=>$id])->row();
$config['upload_path']      = './upload/';
$config['allowed_types']    = 'pdf';
$config['max_size']         = 20000;

$this->load->library('upload', $config);
if(!$this->upload->do_upload('pdf_file')){
$error = array('error' => $this->upload->display_errors());
$this->load->view('apply_form', $error);
}else{
$data_upload = $this->upload->data();
$company_id = $this->input->post('company_id');
$user_name  = $this->session->userdata('name');
$user_desc  = $this->input->post('user_desc');
$ktp_file   = $this->m_lowongan->upload_ktp($this->input->post('ktp_file'));
$data = array(
'user_name'     => $user_name,
'company_id'    => $company_id,
'job_title'     => $lowongan->title,
'user_desc'     => $user_desc,
'pdf_file'      => $data_upload['file_name'],
'ktp_file'      => $ktp_file,
'apply_status'  => 'pending'
);
$where = array(
'company_id'    => $company_id,
);
$this->m_lowongan->apply_data($where, $data, 'apply_job');

redirect('dashboard');
}
}

The Model:

public function upload_ktp(){
$config['upload_path']      = './upload/';
$config['allowed_types']    = 'jpg|jpeg|png';
$config['max_size']         = 20000;
$this->load->library('upload', $config);
if($this->upload->do_upload('ktp_file')){
return $this->upload->data('file_name');  
}else{
return "defaultimg.png";
}
}

我试着这样使用视图:

<tr>
<td>Silahkan Upload File CV anda : .JPG|JPEG|PNG</td>
<td>
<input type="file" name="ktp_file[]" id="ktp_file" multiple>
</td>
<?php if (isset($error)) : ?>
<div class="invalid-feedback"><?= $error ?></div>
<?php endif; ?>
</tr>

但是它给了我这个错误:is_uploaded_file(): Argument #1 ($filename) must be of type string, array given

我解决了。

使用if(!empty($_FILES[]))

所以我把我的代码改成:

public function store_apply($id){
$lowongan = $this->db->get_where('lowongan', ['id'=>$id])->row();
$config['upload_path']      = './upload/';
$config['allowed_types']    = 'pdf|jpg|jpeg|png';
$config['max_size']         = 20000;

$this->load->library('upload', $config);
//CV
if(!empty($_FILES['pdf_file'])){
$this->upload->do_upload('pdf_file');
$data_cv = $this->upload->data();
$pdf_file =  $data_cv['file_name'];
}
//KTP
if(!empty($_FILES['ktp_file'])){
$this->upload->do_upload('ktp_file');
$data_ktp = $this->upload->data();
$ktp_file = $data_ktp['file_name'];
}
$company_id = $this->input->post('company_id');
$user_name  = $this->session->userdata('name');
$user_desc  = $this->input->post('user_desc');
$data = array(
'user_name'     => $user_name,
'company_id'    => $company_id,
'job_title'     => $lowongan->title,
'user_desc'     => $user_desc,
'pdf_file'      => $pdf_file,
'ktp_file'      => $ktp_file,
'apply_status'  => 'pending'
);
$where = array(
'company_id'    => $company_id,
);
$this->m_lowongan->apply_data($where, $data, 'apply_job');

redirect('dashboard');
}