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

kknvjkwl  于 2023-04-03  发布在  其他
关注(0)|答案(1)|浏览(153)

我有一个申请表,一个工人,想申请一份工作,并已上传简历格式的PDF文件,和他的身份证格式的JPG|JPEG|PNG文件。如何上传这2种不同的文件格式?
我的观点:

<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>

主计长:

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');
        }
    }

模型:

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

13z8s7eq

13z8s7eq1#

我解决了。
使用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');
    }

相关问题