-2

i am trying to do multiple image upload with different file input with the following code

<input name="attachement[]" type="file">
<input name="attachement[]" type="file">

My codeigniter code is

$config = array(
                'upload_path' => $path,
                'max_size' => 1024 * 100,
                'allowed_types' => 'gif|jpeg|jpg|png',
                'overwrite' => true,
                'remove_spaces' => true);
            $images = array();
            $this->load->library('upload');

            $files = $_FILES;
            $count = count($_FILES[$attachName]['name']);


            for ($i = 0; $i < $count; $i++) {

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

                $fileName = $title . '_' . $_FILES[$attachName]['name'];
                $images[] = $fileName;
                $config['file_name'] = $fileName;

                $this->upload->initialize($config);
                $this->upload->do_upload();
                if ($this->upload->do_upload()) {
                    $return['data'][] = $this->upload->data();
                    $return['status'] = "success";
                    $return['msg'] = sprintf($this->lang->line('success_item_added'), "Image", "uploaded");
                } else {
                    $return['status'] = 'danger';
                    $return['msg'] .= $this->upload->display_errors('', '') . "\r";
                }

            }

In the code above $attachName="attachement"

But i get error You did not select any file to upload

what am i doing wrong?

Smith
  • 5,765
  • 17
  • 102
  • 161

3 Answers3

3

I found the solution to my problem

$this->upload->do_upload(); <-- removed this line

i changed this line

if ($this->upload->do_upload()) {

to this

if ($this->upload->do_upload($attachName)) {

I hope this helps someone

Thanks to those who downvoted without a comment or even why

Smith
  • 5,765
  • 17
  • 102
  • 161
  • Hi, i used $attachName, i am getting error like "Variable not found"....How to solve this. – Vijaykarthik May 21 '15 at 13:52
  • $attachName is a variable, it is the name of the upload field in the form – Smith May 22 '15 at 12:36
  • Hi, i used my upload file name alone i dint used any variable. If i add 3 images means, only 1 image adding to db and the given folder, remaining two image was not adding. To see my code [click this link](http://stackoverflow.com/questions/30381465/how-to-add-multiple-files-in-codeigniter?noredirect=1#comment48853855_30381465).......please give me solution for this i am struggling for two days – Vijaykarthik May 22 '15 at 12:52
0

For multiple file selection and upload you have to use

 <input name="attachement[]" type="file" multiple>

Otherwise always it will choose one file and multiple file fileds with same will overwrite each other.

Neeraj Kumar
  • 1,058
  • 1
  • 9
  • 22
0

Please include enctype="multipart/form-data this in your <form> tag as:

 <form method="post" enctype="multipart/form-data">
       <input name="attachement[]" type="file">
       <input name="attachement[]" type="file">
    </form>