1

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);
} 
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • Welcome to Stack Overflow! I edited your question to try to improve its impact, modifying the title, improving the code format (indentation is a big issue here on SO) and made some minor grammar/spelling corrections. I hope you approve. – Adrian Mole Sep 28 '19 at 20:50
  • This question has been answered many times. Simply search for "codeigniter multiple image upload" and you'll get more than a few working answers. Possible duplicate of [Multiple files upload in Codeigniter](https://stackoverflow.com/questions/20113832/multiple-files-upload-in-codeigniter) – Alex Sep 29 '19 at 07:41

1 Answers1

0

i found the problem in my code in for loop it was upload one file after that redirected to index page ( this is mistake ) case the redirect line must be outside for loop :)

Here full working code after some modification with able to create folder for each user id (i take the code from stack usere here :) thanks alot )

in upload page i use

 public function upload() { 
$acc = $last_id;
        $file_path = ".uploads/images/" . $acc . '/';

        if (isset($_FILES['multipleUpload'])) {

            if (!is_dir('uploads/images/' . $acc)) {
                mkdir('.uploads/images/' . $acc, 0777, TRUE);
            }

            $files = $_FILES;
            $cpt = count($_FILES ['multipleUpload'] ['name']);
            $this->load->library('upload');
            for ($i = 0; $i < $cpt; $i ++) {

                $name = $files ['multipleUpload'] ['name'] [$i];
                $_FILES ['multipleUpload'] ['name'] = $name;
                $_FILES ['multipleUpload'] ['type'] = $files ['multipleUpload'] ['type'] [$i];
                $_FILES ['multipleUpload'] ['tmp_name'] = $files ['multipleUpload'] ['tmp_name'] [$i];
                $_FILES ['multipleUpload'] ['error'] = $files ['multipleUpload'] ['error'] [$i];
                $_FILES ['multipleUpload'] ['size'] = $files ['multipleUpload'] ['size'] [$i];

                $this->upload->initialize($this->set_upload_options($file_path));

                if(!($this->upload->do_upload('multipleUpload')) || $files ['multipleUpload'] ['error'] [$i] !=0)
                {
                    print_r($this->upload->display_errors());
                }
                else
                {

                    $this->accidents_model->addphoto($name,$acc);



                }

        }            
    //======================================================================================
     }
     $this->session->set_flashdata('success', 'the files uploaded');        
        redirect('accidents/index');   // :) here must located outside for loop
       }
      }



            public function set_upload_options($file_path) {
            // upload an image options
             $config = array();
             $config ['upload_path'] = $file_path;
             $config ['allowed_types'] = 'gif|jpg|png';
             return $config;
            }