3

In codeigniter 2 I have to do a multiple file upload.

In my view input elements looks like this

<input type="file" name="file[]" id="file_1" />
<input type="file" name="file[]" id="file_2" />
<input type="file" name="file[]" id="file_3" />
<input type="file" name="file[]" id="file_4" />
<input type="file" name="file[]" id="file_5" />
<input type="file" name="file[]" id="file_6" />

Plese help me how to write the controller to upload these files .. googled a lot .. Thanks in advance

ramesh
  • 4,008
  • 13
  • 72
  • 117
  • 1
    and what did Google say? What have you tried? Wat does $_FILES say? – giorgio Feb 14 '12 at 12:21
  • 1
    Tried anything? Post your code so far. Also, you can find many similar questions here on SO; doing a multiple upload witht he native upload class is almost just a matter of using a loop. For example, http://stackoverflow.com/questions/1908247/codeigniter-multiple-file-upload – Damien Pirsy Feb 14 '12 at 12:24
  • in my controller i just wrote $images=$_FILES['file']; $res=$this->admins->addPlace($insertdata,$images); and it just send it to my admins model. There I am stucked with the loop .. please help – ramesh Feb 14 '12 at 12:24

1 Answers1

6

You can upload any number of files

$config['upload_path'] = 'upload/Main_category_product/';
$path=$config['upload_path'];
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '1024';
$config['max_width'] = '1920';
$config['max_height'] = '1280';
$this->load->library('upload', $config);

foreach ($_FILES as $key => $value) {

    if (!empty($value['tmp_name']) && $value['size'] > 0) {

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

            $errors = $this->upload->display_errors();
            flashMsg($errors);

        } else {
            // Code After Files Upload Success GOES HERE
        }
    }
}

And try using HTML like this:

<input type="file" name="file1" id="file_1" />
<input type="file" name="file2" id="file_2" />
<input type="file" name="file3" id="file_3" />
Waqleh
  • 9,741
  • 8
  • 65
  • 103
srbhbarot
  • 1,317
  • 12
  • 16
  • Hi thanks for the reply and I am getting this error... Please help A PHP Error was encountered Severity: Notice Message: Array to string conversion Filename: libraries/Upload.php Line Number: 161 – ramesh Feb 14 '12 at 13:14
  • Hi srbhbarot .. Please find my below answer .. The error I got .. Please help – ramesh Feb 14 '12 at 13:22
  • Sorry I cant answer my self .. Now I am getting this error You did not select a file to upload. – ramesh Feb 14 '12 at 13:23
  • Always try a code with low level of test data. First change your name of file tags to anything file1,file2.... and then try with minimum size of images. – srbhbarot Feb 14 '12 at 13:29
  • This is not the correct solution. The Upload library uses the $_FILES superglobal for validation and error handling, and always treats it as a single file upload. – Pjottur Jul 08 '13 at 19:21
  • This is not a good answer, just copy paste from another different question. Check out my answer here, there is no need to change html: http://stackoverflow.com/questions/14863179/uploading-images-in-codeigniter-is-userfile-required/36943931#36943931 – Rauli Rajande May 03 '16 at 09:30