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

3
  • 1
    and what did Google say? What have you tried? Wat does $_FILES say? Commented Feb 14, 2012 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, stackoverflow.com/questions/1908247/… Commented Feb 14, 2012 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 Commented Feb 14, 2012 at 12:24

1 Answer 1

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" />
Sign up to request clarification or add additional context in comments.

6 Comments

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
Hi srbhbarot .. Please find my below answer .. The error I got .. Please help
Sorry I cant answer my self .. Now I am getting this error You did not select a file to upload.
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.
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.