I am trying to upload multiple photos! My code is working but it's uploading only one photo - not all selected photos.
What's wrong in my code?
if(count($_FILES["userfile"]["name"]) == 0) {
$this->session->set_flashdata('success', '?? ????? ?????? ?????');
redirect('accidents/index');
}
else {
// configurations from upload library
$config['upload_path'] = './uploads/images';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '2048000'; // max size in KB
$config['max_width'] = '20000'; //max resolution width
$config['max_height'] = '20000'; //max resolution height
// load CI libarary called upload
$this->load->library('upload', $config);
for($count = 0; $count < count($_FILES["userfile"]["name"]); $count++) {
// body of if clause will be executed when image uploading is failed
if(!$this->upload->do_upload()) {
$errors = array('error' => $this->upload->display_errors());
// This image is uploaded by deafult if the selected image in not uploaded
$image = 'no_image.png';
}
// body of else clause will be executed when image uploading is succeeded
else {
$data = array('upload_data' => $this->upload->data());
$image = $_FILES['userfile']['name']; //name must be userfile
}
$this->accidents_model->addphoto($image,$last_id);
}
}
And the model is:
public function addphoto($photo,$last_id) {
$data = array(
'cp_photo' => $photo,
'ac_id' => $last_id
);
//insert image to the database
$this->db->insert('cars_photos', $data);
}