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

在CodeIgniter中用AJAX和PHP上传多图遇问题,请求排查

CodeIgniter多图上传问题排查与修复

问题概述

在使用AJAX+PHP(CodeIgniter框架)实现多图上传时,uploadimg()函数返回undefined,无法完成图片上传。以下是代码中的问题及修复方案:


前端代码问题与修复

1. 选中文件时仅捕获单个文件

原代码中var file = this.files[0];只取了第一个选中的文件,忽略了multiple属性允许的多个文件,导致selectedImages数组无法收集所有选中图片。

修复代码:

$("#images").change(function () {
  // 循环所有选中的文件
  for (var i = 0; i < this.files.length; i++) {
    var file = this.files[i];
    if (file) {
      // 原有的预览逻辑保持不变
      $("#image-preview-container").append(imageContainer);
      // 将所有文件加入数组
      selectedImages.push(file);
    }
  }
  $(this).val("");
});

2. AJAX点击事件缺少event参数

原代码中$('#addcart').click(function () {未传入event参数,执行event.preventDefault()会报错,导致表单可能默认提交。

修复代码:

$('#addcart').click(function (event) { // 添加上event参数
  event.preventDefault();
  // 后续AJAX逻辑保持不变
});

3. 无图片时错误的FormData设置

原代码中无图片时执行formData.append("images[]", []);,会导致后端$_FILES结构异常,直接删除该分支即可。

修复后的AJAX部分代码:

if (selectedImages.length > 0) {
    for (var i = 0; i < selectedImages.length; i++) {
        var image = selectedImages[i];
        if (image instanceof File) {
          formData.append("images[]", image);
        }
    }
}
// 移除else分支

后端CodeIgniter代码问题与修复

1. uploadimg()函数中多图上传的do_upload参数错误

原代码中$this->upload->do_upload('images')无法识别多张图片的单个文件,需要指定当前循环的文件索引。

2. 重复加载Upload库

每次循环都执行$this->load->library('upload', $config);会导致资源浪费,应该只加载一次,循环时重置配置。

3. 自定义文件名与encrypt_name冲突

$config['encrypt_name'] = TRUE;会覆盖自定义的$file_name,需要移除该配置。

4. 未检查$_FILES是否存在

无图片时$_FILES['images']不存在,直接访问会报错,需要先判断。

修复后的addNewCert()函数:

private function addNewCert(){
    $issuesBody = $this->input->post('issuesBody'); 
    $uploadedFiles = [];
    // 先判断是否有上传的文件
    if(isset($_FILES['images']) && !empty($_FILES['images']['name'][0])){
        $images = $_FILES['images']; 
        $uploadedFiles = $this->uploadimg($images);
    }

    if ($uploadedFiles === false) {
        // 错误处理逻辑
        echo json_encode($output);
        return;
    } 
    // 后续逻辑
}

修复后的uploadimg()函数:

private function uploadimg($images) {
    $uploadedFiles = array();
    // 先加载一次Upload库
    $this->load->library('upload');

    for ($i = 0; $i < count($images['name']); $i++) {
        // 跳过空文件(避免用户选中后取消的情况)
        if(empty($images['name'][$i])) continue;
        
        $originalFileName = $images['name'][$i];
        $file_name = uniqid() . '_' . $originalFileName;

        $config['upload_path'] = $this->uploadPath;
        $config['file_name'] = $file_name;
        $config['allowed_types'] = 'jpg|jpeg|png|gif'; 
        $config['max_size'] = 2048; 
        // 移除encrypt_name配置,避免覆盖自定义文件名
        // $config['encrypt_name'] = TRUE; 

        // 重置上传配置
        $this->upload->initialize($config);

        // 指定当前要上传的文件字段(多张图的单个项)
        if ($this->upload->do_upload('images['.$i.']')) {
            $uploadedFiles[] = $file_name;
        } else {
            // 可以打印错误信息排查:$this->upload->display_errors();
            return false;
        }
    }

    return $uploadedFiles;
}

5. 检查上传目录权限

确保$this->uploadPath指向的目录存在且有写入权限(通常设置为755或777,根据服务器配置调整)。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 22:44:55