You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在CodeIgniter单个表单中上传PDF与图片文件

实现PDF简历与图片身份证的分格式上传及错误修复

问题概述

求职者申请表单需分别上传PDF格式简历和JPG/JPEG/PNG格式身份证,修改身份证上传控件为多文件模式(name="ktp_file[]")后,出现错误:

is_uploaded_file():参数#1($filename)必须是字符串类型,传入的是数组

错误原因

CodeIgniter的do_upload()方法默认仅支持单文件上传字段(字符串类型字段名),当字段名改为数组格式(如ktp_file[])时,原有单文件上传逻辑无法识别;同时原控制器中错误地使用$this->input->post('ktp_file')传递文件参数,文件上传需直接操作文件字段而非POST参数。


修复方案

方案1:单身份证文件上传(常规场景)

如果仅需上传一张身份证,恢复单文件逻辑即可:

1. 视图代码修正

修正控件文本错误,添加前端类型过滤,移除多文件属性:

<tr>
 <td>Silahkan Upload File KTP anda : .JPG|JPEG|PNG</td>
 <td>
  <input type="file" name="ktp_file" id="ktp_file" accept="image/jpg,image/jpeg,image/png">
 </td>
 <?php if (isset($error)) : ?>
  <div class="invalid-feedback"><?= $error ?></div>
 <?php endif; ?>
</tr>

accept属性可在前端过滤文件类型,提升用户体验

2. 控制器代码修正

移除错误的POST参数传递,直接调用模型上传方法:

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');
        // 修正:直接调用模型上传方法,无需传递POST参数
        $ktp_file   = $this->m_lowongan->upload_ktp();

        $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条件,直接传入数据即可
        $this->m_lowongan->apply_data($data, 'apply_job');
        
        redirect('dashboard');
    }
}

3. 模型代码优化

添加配置初始化,避免与控制器的上传库配置冲突:

public function upload_ktp(){
    $config['upload_path']      = './upload/';
    $config['allowed_types']    = 'jpg|jpeg|png';
    $config['max_size']         = 20000;

    // 重新初始化上传配置,避免冲突
    $this->upload->initialize($config);

    if($this->upload->do_upload('ktp_file')){
        return $this->upload->data('file_name');  
    }else{
        return "defaultimg.png";
    }
}

方案2:多张身份证上传(特殊场景)

若需支持多张身份证上传,需调整为多文件处理逻辑:

1. 视图代码

保留多文件属性:

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

2. 控制器代码

处理多文件上传返回的文件名数组,转为字符串存入数据库:

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_files   = $this->m_lowongan->upload_multiple_ktp();
        // 将文件名数组转为逗号分隔字符串
        $ktp_file_str = implode(',', $ktp_files);

        $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_str,
            'apply_status'  => 'pending'
        );

        $this->m_lowongan->apply_data($data, 'apply_job');
        
        redirect('dashboard');
    }
}

3. 模型代码:新增多文件上传方法

public function upload_multiple_ktp(){
    $config['upload_path']      = './upload/';
    $config['allowed_types']    = 'jpg|jpeg|png';
    $config['max_size']         = 20000;

    $this->load->library('upload', $config);
    $uploaded_files = array();

    // 遍历处理多文件
    foreach($_FILES['ktp_file']['name'] as $key => $name){
        $_FILES['ktp_file_single']['name'] = $_FILES['ktp_file']['name'][$key];
        $_FILES['ktp_file_single']['type'] = $_FILES['ktp_file']['type'][$key];
        $_FILES['ktp_file_single']['tmp_name'] = $_FILES['ktp_file']['tmp_name'][$key];
        $_FILES['ktp_file_single']['error'] = $_FILES['ktp_file']['error'][$key];
        $_FILES['ktp_file_single']['size'] = $_FILES['ktp_file']['size'][$key];

        $this->upload->initialize($config);
        if($this->upload->do_upload('ktp_file_single')){
            $uploaded_files[] = $this->upload->data('file_name');
        }
    }

    // 无成功上传文件时返回默认图
    return empty($uploaded_files) ? array("defaultimg.png") : $uploaded_files;
}

内容的提问来源于stack exchange,提问作者Nicho

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 09:22:57