如何通过更新Codeigniter中的现有图像路径(不替换)来向db添加更多图像

时间:2016-02-01 18:46:32

标签: php codeigniter codeigniter-3

在这里寻求帮助。

我可以添加更多图像到上传文件夹以及图像路径到数据库,但实际上替换了我想要保留的以前上传的图像(图像保留在上传文件夹中但数据库中的图像路径更改)。

我在db中的现有图像路径是内嵌形式(image1jpg,image2.jpg,..,)

我的模特:

function add_more_images_to_listing($data, $id) {
  $data = array(
    'property_images' => $data['images'],
  );
  $this->db->where('property_ref_id', $id);
  $query=$this->db->update('vbc_property_images', $data);
  return $query;
}

控制器:

$id = $this->uri->segment(3);

$number_of_files = count($_FILES['uploadedimages']['tmp_name']);
$files = $_FILES['uploadedimages'];
for($i=0;$i<$number_of_files;$i++) {
  if($_FILES['uploadedimages']['error'][$i] != 0) {
    $this->form_validation->set_message('fileupload_check', 'At least 1 image needs to be uploaded in jpeg, png or gif format only.');
    return FALSE;
  }
}

$this->load->library('upload');
$config['upload_path'] = FCPATH . 'uploads/property-images';
$config['allowed_types'] = 'gif|jpg|png';
$config['file_name']      = 'property_image_1';
$config['max_size']      = '0';
$config['overwrite']     = FALSE;
for ($i = 0; $i < $number_of_files; $i++) {
  $_FILES['uploadedimage']['name'] = $files['name'][$i];
  $_FILES['uploadedimage']['type'] = $files['type'][$i];
  $_FILES['uploadedimage']['tmp_name'] = $files['tmp_name'][$i];
  $_FILES['uploadedimage']['error'] = $files['error'][$i];
  $_FILES['uploadedimage']['size'] = $files['size'][$i];

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

  if ($this->upload->do_upload('uploadedimage', $i)) {
    $data['uploadedimage'] = $this->upload->data();
    $image_name[$i] = $data['uploadedimage']['file_name'];
    $data['images'] = implode(',',$image_name);
  } else {
    $this->form_validation->set_message('fileupload_check', $this->upload->display_errors());
    return FALSE;
  }
}

感谢。

1 个答案:

答案 0 :(得分:0)

我只是把它弄出来了。我在模型中添加了:

    $this->db->select('property_images');
    $this->db->from('vbc_property_images');
    $this->db->where('property_ref_id', $id);
    $query = $this->db->get();
    $query_result = $query->result_array();
    if (empty($query_result)) {
    return FALSE;
}
    elseif (count($query_result) > 1) {
    return 0;
}
    else{
    $rowId = $query_result[0]['property_images'];
    //return $rowId;    
}

并替换:

'property_images' => $data['images'],

'property_images' => $rowId.','.$data['images'],

似乎现在正在工作。