CodeIgniter多图片上传无报错但失败问题求助
CodeIgniter 多图片上传无报错但无法上传问题排查
问题描述
我在CodeIgniter中实现多图片上传功能,设置了15个可选图片字段,名称从pimage到pimage15。控制器与HTML代码如下,服务器权限、PHP上传限制等配置均正常,但无报错信息却无法完成文件上传。
控制器代码
public function editprojectplans() { $data = array(); if ($this->isUserLoggedIn) { $con = array( 'id' => $this->session->userdata('userId') ); $data['user'] = $this->user->getRows($con); $id = $this->uri->segment(3); $data['vals'] = $this->category->selectproject($id); if (isset($_POST['editproduct'])) { // Check whether user uploaded pictures $uploadPath = './uploads/products/'; $width = 1000; // Define width as per your requirement $height = 1000; // Define height as per your requirement for ($i = 1; $i <= 15; $i++) { $fileInputName = 'pimage'.$i; if (!empty($_FILES[$fileInputName]['name'])) { $uploadImgData[$i] = $this->processImageUpload($fileInputName, $uploadPath, $width, $height); } } // 后续代码省略 } } } private function processImageUpload($field, $uploadPath, $width, $height) { $this->load->library('upload'); $this->load->library('image_lib'); $imageData = array(); $ImageCount = count($_FILES[$field]['name']); for ($i = 0; $i < $ImageCount; $i++) { $_FILES['file']['name'] = $_FILES[$field]['name'][$i]; $_FILES['file']['type'] = $_FILES[$field]['type'][$i]; $_FILES['file']['tmp_name'] = $_FILES[$field]['tmp_name'][$i]; $_FILES['file']['error'] = $_FILES[$field]['error'][$i]; $_FILES['file']['size'] = $_FILES[$field]['size'][$i]; // File upload configuration $config['upload_path'] = $uploadPath; $config['allowed_types'] = 'jpg|jpeg|png|gif'; $this->upload->initialize($config); // Upload file to server if ($this->upload->do_upload('file')) { // Uploaded file data $imageData[] = $this->upload->data(); // Resize uploaded image $path = $imageData[$i]['full_path']; $config['image_library'] = 'gd2'; $config['source_image'] = $path; $config['maintain_ratio'] = TRUE; $config['width'] = $width; $config['height'] = $height; $this->image_lib->initialize($config); $this->image_lib->resize(); $this->image_lib->clear(); } else { // Handle upload error $error = array('error' => $this->upload->display_errors()); // You can log or handle the error as per your application's requirement } } return $imageData; }
HTML代码
<form action="" method="post" enctype="multipart/form-data" id="myform"> <input type="file" accept="image/png, image/gif, image/jpeg" class="w-full px-4 py-2 placeholder-secondary-400 border dark:bg-dark-card dark:border-secondary-800 rounded outline-none focus:border-primary-500 dark:focus:border-primary-500 focus:shadow" name="pimage[]"> <input type="hidden" name="pimage1p" value="<?=$val->floorplan1?>" /> <input type="file" accept="image/png, image/gif, image/jpeg" class="w-full px-4 py-2 placeholder-secondary-400 border dark:bg-dark-card dark:border-secondary-800 rounded outline-none focus:border-primary-500 dark:focus:border-primary-500 focus:shadow" name="pimage2[]"> <input type="hidden" name="pimage2p" value="<?=$val->floorplan2?>" /> <!-- 中间pimage3到pimage14字段省略 --> <input type="file" accept="image/png, image/gif, image/jpeg" class="w-full px-4 py-2 placeholder-secondary-400 border dark:bg-dark-card dark:border-secondary-800 rounded outline-none focus:border-primary-500 dark:focus:border-primary-500 focus:shadow" name="pimage15[]"> <input type="hidden" name="pimage15p" value="<?=$val->floorplan15?>" /> </form>
排查与修复方案
字段名称不匹配
HTML中第一个文件字段名称为pimage[],但控制器循环从pimage1开始遍历,导致第一个字段的文件完全无法被处理。需将第一个字段的name="pimage[]"改为name="pimage1[]",确保与控制器的pimage1对应。库重复加载问题
processImageUpload方法中每次循环都重新加载upload和image_lib库,可能导致内部状态异常。应将库加载移到方法开头,循环内仅重新初始化配置:private function processImageUpload($field, $uploadPath, $width, $height) { // 移到方法开头加载库,避免重复加载 $this->load->library('upload'); $this->load->library('image_lib'); $imageData = array(); // 后续代码不变 }空文件判断逻辑缺失
控制器中!empty($_FILES[$fileInputName]['name'])的判断不够严谨,因为$_FILES[$field]['name']是数组,空数组也会被判定为非空。需修改为判断数组中是否有有效文件名:for ($i = 1; $i <= 15; $i++) { $fileInputName = 'pimage'.$i; if (!empty($_FILES[$fileInputName]['name']) && !empty(array_filter($_FILES[$fileInputName]['name']))) { $uploadImgData[$i] = $this->processImageUpload($fileInputName, $uploadPath, $width, $height); } }同时在
processImageUpload的循环中跳过空文件:for ($i = 0; $i < $ImageCount; $i++) { // 跳过空文件 if (empty($_FILES[$field]['name'][$i])) { continue; } // 后续代码不变 }错误日志缺失
当前错误处理仅定义了$error变量但未记录,无法排查隐藏错误。需在else分支添加日志记录:} else { // 记录错误日志到CodeIgniter日志文件 log_message('error', '上传字段'.$field.'的第'.$i.'个文件失败:'.$this->upload->display_errors()); }
内容的提问来源于stack exchange,提问作者jbedufly app
相关产品推荐
相关产品推荐

