0

I have two input field as below;

<input type="file" name="datainfo[site_logo]" class="custom-file-input" id="site_logo">
<input type="file" name="datainfo[site_fav]" class="custom-file-input" id="site_fav">

How can i upload two files with foreach ? I have tried a few methods but i couldnt achieve this.

Thanks,

2

1 Answer 1

0

Ok if you insist to keep it like that with your parameters and input names, then you have to restructure $_FILES array before do_upload():

public function upload()
{
    $count = count($_FILES['datainfo']['name']);
    for ($i=0; $i < $count; $i++)
    {
        foreach ($_FILES['datainfo'] as $key1 => $value1)
        {
            foreach ($value1 as $key2 => $value2)
            {
                $files[$key2][$key1] = $value2;
            }
        }
    }
    $_FILES = $files;
    $config['upload_path'] = FCPATH.'uploads/';
    $config['allowed_types'] = 'gif|jpg|jpeg|png';
    $config['max_size'] = '2048';
    $config['max_width'] = '1920';
    $config['max_height'] = '1280';
    $this->load->library('upload', $config);
    foreach ($_FILES as $fieldname => $fileObject)
    {
        if (!empty($fileObject['name']))
        {
            $this->upload->initialize($config);
            if (!$this->upload->do_upload($fieldname))
            {
                $errors = $this->upload->display_errors();
            }
            else
            {
                 $success = 'Success';
            }
        }
    }
}

I've tested it in my environment with your inputs as it is and it works fine.

Sign up to request clarification or add additional context in comments.

1 Comment

You're welcome bro, glad it works .. you can select it as correct answer.

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.