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

CodeIgniter文件上传至数据库及解决上传类加载失败问题

解决CodeIgniter中“unable to load the upload class”错误并实现图片上传至数据库

首先,你遇到的“unable to load the upload class”错误,核心原因是没有在控制器中正确加载CodeIgniter的Upload类,而且当前的save()方法完全没有处理文件上传的逻辑。下面一步步帮你修正并实现完整的图片上传功能:

1. 确保数据库表包含图片存储字段

首先,你的konfirmasi表需要添加一个用于存储图片的字段,推荐两种方案:

  • 存储图片路径(性能更优,便于后续展示):添加gambar_path VARCHAR(255)字段
  • 存储图片二进制数据:添加gambar BLOB字段

这里我们以存储图片路径为例进行演示。

2. 修改控制器的save()方法

更新你的控制器代码,加入Upload类加载、上传配置、上传验证以及图片路径存入数据库的逻辑:

function save() {
    // 加载CodeIgniter Upload类
    $this->load->library('upload');

    // 1. 获取表单普通字段数据
    $id_pemesanan = $this->input->post('id_pemesanan');
    $nama_pemesan = $this->input->post('nama_pemesan');
    $email = $this->input->post('email');
    $jumlah = $this->input->post('jumlah');
    $tgl_bayar = $this->input->post('tgl_bayar');
    $metode_pembayaran = $this->input->post('metode_pembayaran');

    // 2. 配置上传参数
    $upload_config = array(
        'upload_path'   => './uploads/bukti_transfer/', // 确保此文件夹存在且有写入权限
        'allowed_types' => 'jpg|jpeg|png|gif', // 允许的图片格式
        'max_size'      => 2048, // 最大2MB
        'file_name'     => uniqid() . '_' . $_FILES['gambar']['name'], // 生成唯一文件名避免重复
    );

    $this->upload->initialize($upload_config);

    // 3. 执行上传并验证结果
    if (!$this->upload->do_upload('gambar')) {
        // 上传失败,返回错误提示
        $error_msg = $this->upload->display_errors();
        $this->session->set_flashdata("msg", "<div class='alert bg-danger' role='alert'> <a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a> <svg class='glyph stroked empty-message'><use xlink:href='#stroked-empty-message'></use></svg> 上传失败:{$error_msg} </div>");
        redirect('konfirmasi/konfirmasi_list');
    } else {
        // 上传成功,获取上传文件信息
        $upload_data = $this->upload->data();
        $gambar_path = 'uploads/bukti_transfer/' . $upload_data['file_name']; // 存储相对路径

        // 4. 准备插入数据库的完整数据
        $data = array(
            'id_pemesanan' => $id_pemesanan,
            'nama_pemesan' => $nama_pemesan,
            'email' => $email,
            'jumlah' => $jumlah,
            'tgl_bayar' => $tgl_bayar,
            'metode_pembayaran' => $metode_pembayaran,
            'gambar_path' => $gambar_path // 新增图片路径字段
        );

        // 5. 事务处理插入数据
        $this->db->trans_start();
        $this->db->insert('konfirmasi', $data);
        $this->db->trans_complete();

        if ($this->db->trans_status() === FALSE) {
            // 插入失败,删除已上传的图片
            unlink(FCPATH . $gambar_path);
            $this->session->set_flashdata("msg", "<div class='alert bg-danger' role='alert'> <a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a> <svg class='glyph stroked empty-message'><use xlink:href='#stroked-empty-message'></use></svg> Data gagal tersimpan. </div>");
        } else {
            $this->session->set_flashdata("msg", "<div class='alert bg-success' role='alert'> <a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a> <svg class='glyph stroked empty-message'><use xlink:href='#stroked-empty-message'></use></svg> Data tersimpan. </div>");
        }
        redirect('konfirmasi/konfirmasi_list');
    }
}

关键注意点:

  • 手动创建./uploads/bukti_transfer/文件夹,并设置服务器权限为755或777(确保PHP有写入权限)
  • 如果要存储二进制图片数据,将$gambar_path替换为file_get_contents($upload_data['full_path']),同时数据库字段类型改为BLOB

3. 视图部分的小修正

你的视图中有两个输入框的type属性错误,需要改为text:

<!-- 修正前 -->
<input type="id_pemesanan" name="id_pemesanan" class="form-control" required="required" placeholder="Kode Pemesanan">
<input type="nama_pemesan" name="nama_pemesan" class="form-control" required="required" placeholder="Masukan Nama..">

<!-- 修正后 -->
<input type="text" name="id_pemesanan" class="form-control" required="required" placeholder="Kode Pemesanan">
<input type="text" name="nama_pemesan" class="form-control" required="required" placeholder="Masukan Nama..">

4. 模型补充(可选,用于展示图片)

如果需要在列表页展示图片,确保你的模型查询返回gambar_path字段:

public function datakonfirmasi() {
    $query = $this->db->query('SELECT * FROM konfirmasi');
    return $query->result();
}

常见问题排查

  • 若仍出现“unable to load the upload class”:检查application/libraries/Upload.php是否存在(CodeIgniter默认自带),或者在application/config/autoload.php中添加$autoload['libraries'] = array('upload');实现自动加载
  • 上传失败:检查文件夹权限、文件大小是否超出配置限制、文件格式是否符合要求

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 09:52:53