Not able to insert path for multiple images in dat

2019-08-06 04:20发布

问题:

I can't seem to figure out how to make this multiple image path upload working. I have been trying to fix it for 2 days but no luck.

Problem: when form is submitted it uploads selected numbers of images to 'upload' folder but only inserts path for one image in db table.

// Form Validation Goes Here
} else {
    // Image upload starts here
    $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 needed.');
    return FALSE;
  }
}
    $this->load->library('upload');
    $config['upload_path'] = FCPATH . 'uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $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')) {
    $data['uploadedimage'] = $this->upload->data();
    $image_name = $data['uploadedimage']['file_name'];
    $data['uploadedimage'] = $image_name;
  } else {
    $this->form_validation->set_message('upload_error', $this->upload->display_errors());
    return FALSE;
  }
}
$this->load->model('admin/model_users');
if($query = $this->model_users->insert_property_details($data)) {
redirect('dashboard/property-successfully-posted');
}

Model is:

        $insert_images = array(
        'property_images' => $data['uploadedimage'],
        'property_ref_id'   => $id,
        );
        $this->db->insert('vbc_property_images', $insert_images);

And field name in view file is 'uploadedimage'.

<input type="file" name="uploadedimages[]" accept="image/*" multiple />

回答1:

$data['uploadedimage'] should be in foreach( $data['uploadedimage'] as $key==>$val) loop for read multiple data.and use 'property_images' => $val for insert data



回答2:

  1. $data['uploadedimage'] = $image_name; replace with
  2. $data['uploadedimage'][] = $image_name;


回答3:

I was able to achieve it by replacing in controller:

if ($this->upload->do_upload('uploadedimage')) {
    $data['uploadedimage'] = $this->upload->data();
    $image_name = $data['uploadedimage']['file_name'];
    $data['uploadedimage'] = $image_name;
}

by

if ($this->upload->do_upload('uploadedimage', $i)) {
    $data['uploadedimage'] = $this->upload->data();
    $image_name[$i] = $data['uploadedimage']['file_name'];
    $data['images'] = implode(',',$image_name);
}

And in model:

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

by

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