0

I want to have a multiple file upload code.

For example:

Koala.jpg
Penguins.jpg
Jellyfish.jpg

There is input text where the user can set the new name of the image.

The user will now upload the images and the inputted text for new image name is "Animals"

Now, what I want is when this uploaded the output should be Animals1.jpg, Animals2.jpg, Animals3.jpg.

The problem is when I tried to upload all these images, only one image is uploading.

I tried to make research and applied some codes on my program, but still not working.

Controller

public function do_upload() { 
    $config = array(
        'image_library' => 'gd2',
        'file_name'     => $this->input->post('finame'),
        'upload_path'   => './public/img/uploads',
        'upload_url'    => base_url().'public/img/uploads',
        'allowed_types' => 'gif|jpg|jpeg',
        'max_size'      => '1024KB',
        'max_width'     => '1024',
        'max_height'    => '768',
        'maintain_ratio'=> TRUE,
        'overwrite'     => false,
    );
    $this->load->library('upload', $config);
    if (!$this->upload->do_upload()) {
        $error_msg = "<div class='alert alert-error'>".$this->upload->display_errors()."</div>";
        $error = array('error' => $error_msg);
    } 
    else {
        $upload_data = $this->upload->data();
        $data['thumbnail_name'] = $upload_data['raw_name']. '_thumb' .$upload_data['file_ext'];
        $file_array = array(
            'image'         => $data['thumbnail_name'],
            'image_name'    => $upload_data['file_name'],
            //'description'   => "",
            'date_created'  => date('Y-m-d H:i:s', now()),
            'date_modified' => date('Y-m-d H:i:s', now()),
            'author'        => $this->session->userdata('username'),
            'size'          => $upload_data['file_size'],
            'type'          => $upload_data['image_type'],
            'width'         => $upload_data['image_width'],
            'height'        => $upload_data['image_height'],
            //'document_name' => $field,
            //'department'    => $field2,
            //'notes'         => "",
        );
        $this->session->set_userdata('image_print', $file_array);
        $this->load->database();
        $this->db->insert('tbl_image', $file_array);
        $data = array('upload_data' => $this->upload->data());
        $user_level['records']=$this->user_model->get_records();
        $this->load->view('v_dashboard/page/header_view', $user_level);
        $this->load->view('v_document/upload/upload_result_view', $data);
        $this->load->view('v_dashboard/page/footer_view');
    }
}

I have this on my HTML

<label for="file"><strong>Select File To Upload:</strong></label>
<input type="file" name="userfile[]" multiple class="btn transcolor btn-file"/>
<br/><br/>

By the way, I'm using BLOB on my database.

I tried to refer to this links

Ellislab

GitHub

StackOverflow

StackOverflow

CodingLikeASir

Community
  • 1
  • 1
jned29
  • 477
  • 12
  • 50

1 Answers1

0

You have to run the for loop till the count of the uploaded image file.

Like this:

for ($i=0; $i < count($_FILES['userfile']['name']); $i++)
{ 
  // function to add the image name one by one into database.
}
Pathik Vejani
  • 4,263
  • 8
  • 57
  • 98