我是使用PHP CI框架的新手。在我的项目中,我有多个上传输入。
<form method="post" action="<?php echo site_url('upload_img/om_telolet_om'); ?>">
<input type="text" id="student_id" name="student_id" />
<input type="text" id="arr_img_student" name="arr_img_student" />
<input class="up_img" type="file" name="image1" /><br />
<input class="up_img" type="file" name="image2" />
<input type="submit" value="submit" />
</form>
然后,我使用jQuery填充up_img
的值并将其放在arr_img_student
中。然后将数据发送到控制器。这是控制器:
//UPLOAD
public function om_telolet_om()
{
$this->load->helper('url');
//UPLOAD FILE RULES
$config['upload_path'] = './asset/images/uploads/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '0';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['overwrite'] = TRUE;
$this->load->library('upload', $config);
$this->upload->initialize($config);
for($x = 1; $x<=2; $x++){
$value = "image".$x;
if ( ! $this->upload->do_upload($value)){
$data['error'] = $this->upload->display_errors();
$this->load->view('f_upload', $data);
}
else
{
$data['id'] = $this->input->post('student_id');
$data['img_path'] = $this->input->post('arr_img_student');
$this->model_student->insert_student($data);
}
}
}
代码循环上传但循环插入过程。 如何使代码循环上传(检查上传规则),但只保存一次到数据库?如果上传规则匹配,则应保存数据。在我的数据库中,只使用了一个表。 student_id
和img_path
。我没有权限更改数据库结构。
答案 0 :(得分:0)
$images = array(); // an array to store uploaded images in
for($x = 1; $x<=2; $x++){
$value = "image".$x;
if ( ! $this->upload->do_upload($value)){
$data['error'] = $this->upload->display_errors();
$this->load->view('f_upload', $data);
}
else
{
$filedata = $this->upload->data();
// Every time a file is uploaded,
// add it to the array for saving later
$images[] = $filedata['full_path'];
}
}
$data['id'] = $this->input->post('student_id');
$data['img_path'] = json_encode($images); // Encode $images array to string
$this->model_student->insert_student($data);
稍后当使用表中的数据时,您必须将img_path
解码回数组:
$student_images = json_decode($data['img_path'], true);