CodeIgniter单元素多图上传报错:提示未选择文件,求排查
问题排查与解决方案
你遇到的核心问题是多文件上传时的数组处理逻辑错误,以及CodeIgniter默认上传类不支持批量上传,还有数据库存储字段的适配问题,以下是具体修复步骤:
一、修正控制器中的文件判断与上传逻辑
原代码中直接判断$_FILES['featured_image']['name'] == ""是错误的——当多文件上传时,$_FILES['featured_image']['name']是一个数组,而非字符串,所以这个判断永远会触发“未选择文件”的错误。同时需要循环处理每个上传的文件:
public function create_property_ajax() { $this->form_validation->set_rules('title', 'Title', 'trim'); $this->form_validation->set_rules('description', 'Description', 'trim|min_length[2]|max_length[800]'); $this->form_validation->set_rules('price', 'Price', 'required'); $this->form_validation->set_rules('state', 'State'); $this->form_validation->set_rules('lga', 'LGA', 'required'); $this->form_validation->set_rules('address', 'Address', 'required'); $this->form_validation->set_rules('amenities[]', 'Other Positions'); // 文件上传配置 $config['upload_path'] = 'assets/uploads/properties'; $config['allowed_types'] = 'jpg|jpeg|png'; $config['max_size'] = 1024 * 4; $config['file_ext_tolower'] = TRUE; $config['remove_spaces'] = TRUE; $config['detect_mime'] = TRUE; $this->load->library('upload', $config); if ($this->form_validation->run()) { // 修正:判断是否有文件上传(处理数组情况) $has_files = !empty($_FILES['featured_image']['name'][0]); if (!$has_files) { $this->session->set_flashdata('status_msg_error', 'No file selected.'); redirect(site_url('properties/new_property')); } $uploaded_files = []; $upload_errors = []; // 循环处理每个上传的文件 foreach ($_FILES['featured_image']['name'] as $key => $name) { // 临时重命名$_FILES数组,适配CodeIgniter上传类 $_FILES['single_file']['name'] = $_FILES['featured_image']['name'][$key]; $_FILES['single_file']['type'] = $_FILES['featured_image']['type'][$key]; $_FILES['single_file']['tmp_name'] = $_FILES['featured_image']['tmp_name'][$key]; $_FILES['single_file']['error'] = $_FILES['featured_image']['error'][$key]; $_FILES['single_file']['size'] = $_FILES['featured_image']['size'][$key]; if ($this->upload->do_upload('single_file')) { $file_data = $this->upload->data(); $uploaded_files[] = $file_data['file_name']; // 生成缩略图(如果需要每个图都生成) generate_image_thumb($file_data['file_name'], '500', '500'); } else { $upload_errors[] = $this->upload->display_errors(); } } // 检查是否有上传错误 if (!empty($upload_errors)) { $error = array('error' => implode('<br>', $upload_errors)); $this->new_property($error); } else { // 将多个文件名拼接成字符串(用逗号分隔) $featured_images = implode(',', $uploaded_files); // 这里可以选择只取第一个图的缩略图,或者存所有缩略图,根据需求调整 $first_thumbnail = generate_image_thumb($uploaded_files[0], '500', '500'); $this->property_model->add_new_property($featured_images, $first_thumbnail); $this->session->set_flashdata('status_msg', 'Property added and published successfully.'); redirect(site_url('properties')); } } else { $this->new_property(); } }
二、修改数据库字段与模型代码
你需要先确保数据库的property表中,featured_image字段的类型是TEXT或VARCHAR(255)(如果文件名总长不超过255),用来存储多个文件名的拼接字符串。然后修改模型:
public function add_new_property($featured_images, $thumbnail) { $title = ucwords($this->input->post('title', TRUE)); $description = $this->input->post('description', TRUE); $price = $this->input->post('price', TRUE); $state = ucwords($this->input->post('state', TRUE)); $lga = $this->input->post('lga', TRUE); $address = $this->input->post('address', TRUE); $amenities = implode(", ", $this->input->post('amenities', TRUE)); $data = array( 'title' => $title, 'description' => $description, 'price' => $price, 'state' => $state, 'lga' => $lga, 'address' => $address, 'amenities' => $amenities, 'featured_image' => $featured_images, // 存储多个文件名 'featured_image_thumb' => $thumbnail, // 如果需要多个缩略图,同理拼接 ); $this->db->insert('property', $data); }
三、HTML表单的小优化
原表单的multiple属性可以写成multiple="multiple",不过浏览器也支持空值,另外可以保留required属性确保用户至少选一个文件:
<input type="file" name="featured_image[]" multiple="multiple" class="form-control" accept=".jpeg,.jpg,.png" required />
关键问题总结
- 数组判断错误:多文件上传时
$_FILES['featured_image']的每个子项都是数组,不能直接用== ""判断,要检查name[0]是否为空。 - 批量上传处理:CodeIgniter的
upload类一次只能处理一个文件,需要循环遍历文件数组,临时重构$_FILES来适配。 - 数据库存储:单个字段存储多个文件名需要用分隔符拼接,同时确保字段长度足够。
内容的提问来源于stack exchange,提问作者Sylvester Esso_Nmakwe
相关产品推荐
相关产品推荐

