0

I am trying to upload 2 files through a codeigniter controller. When i select the files and hit submit it always returns error. But when i do a var_dump($_FILES); it shows that the files are passing but not being captured by the codeigniter controller.

Can someone tell me what i am doing wrong? Below is my code

            $config['upload_path']          = './docs/';
            $config['allowed_types']        = 'jpg|doc|docx';
            $config['max_size']             = 10000;
            $config['max_width']            = 3000;
            $config['max_height']           = 3000;

            $this->load->library('upload', $config);
if ( !$this->upload->do_upload('userfile1') || !$this->upload->do_upload('userfile2'))
{

echo "error";

} else {

$f1= $this->upload->data('userfile1');
$f2= $this->upload->data('userfile2');

echo $f1['file_name'];
echo $f2['file_name'];

}

1 Answer 1

1

For multiple file uploading in CI please follow this way --

$config['upload_path']   = 'uploads/photos/'; 
$config['allowed_types'] = 'jpg|jpeg|png|gif'; 
$this->load->library('upload', $config);
for ($i=0; $i < count($_FILES['photos']['name']); $i++) { 
    $_FILES['photos[]']['name']     = $_FILES['photos']['name'][$i];
    $_FILES['photos[]']['type']     = $_FILES['photos']['type'][$i];
    $_FILES['photos[]']['tmp_name'] = $_FILES['photos']['tmp_name'][$i];
    $_FILES['photos[]']['error']    = $_FILES['photos']['error'][$i];
    $_FILES['photos[]']['size']     = $_FILES['photos']['size'][$i]; 
    if ($this->upload->do_upload('photos[]')) {
        $photos_files = array('upload_data' => $this->upload->data()); 
        $photos_arr[] = $photos_files['upload_data']['file_name'];
    }else{
        $error[] = $this->upload->display_errors();
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hi.. Thanks for the reply... I have 2 separate input fields, for 2 different types of documents. So how can i get the files without using the loop?
you can define name attribute value for both input as like -- name="photo[]"

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.