1

I am beginner in CI 3 and I want to upload two files images. I tried google but nothing works. Thanks for all your help

HTML View

<form method="post" accept-charset="utf-8" action="Kasprof" enctype="multipart/form-data">
      <div class="form-group">
            Parent / Potvrdenie zákonného zástupcu
            <input name="images[parent]" type="file">
      </div>
      <div class="form-group">
            Doctor / Potvrdenie od doktora
            <input name="images[doctor]" type="file">
      </div>              
      <button type="submit" class="btn btn-default">Send / Poslať</button>
</form>

PHP Controller:

$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|pdf|jpeg';
$config['max_size'] = "4096000";
$config['max_width'] = "4096";
$config['max_height'] = "4096";
$this->upload->initialize($config);

$this->upload->do_upload($_FILES);

Error:

Message: Illegal offset type in isset or empty
Filename: libraries/Upload.php
Line Number: 377

Message: preg_match_all() expects parameter 2 to be string, array given
Filename: libraries/Upload.php
Line Number: 382
1
  • @Tool I forgot sorry. Commented Jun 9, 2015 at 14:00

3 Answers 3

1

you can use this function to upload multiple upload. where $userfile is input file name, $image_path is your destination path, $allowed allowed types, $max_size maximum allowed upload size.

function _multi_upload_files($userfile,$image_path,$allowed,$max_size)
{
    $this->ci->load->library('upload');
    if(!is_dir($image_path))
    {
        mkdir($image_path);
    }
    $files = $_FILES;
    $cpt = count($_FILES[$userfile]['name']);
    for($i=0; $i<$cpt; $i++)
    {
       if($files[$userfile]['tmp_name'][$i]!='')
       {
            $_FILES[$userfile]['name']= $files[$userfile]['name'][$i];
            $_FILES[$userfile]['type']= $files[$userfile]['type'][$i];
            $_FILES[$userfile]['tmp_name']= $files[$userfile]['tmp_name'][$i];
            $_FILES[$userfile]['error']= $files[$userfile]['error'][$i];
            $_FILES[$userfile]['size']= $files[$userfile]['size'][$i];    

            $config['upload_path'] = $image_path;
            $config['allowed_types'] = $allowed;
            $config['max_size'] = $max_size;
            // if want to rename file
            $img=$_FILES[$userfile]['name'][$i];
            $random_digit=rand(00,99999);
            $ext = strtolower(substr($img, strpos($img,'.'), strlen($img)-1));
            $file_name=$random_digit.$ext;
            $config['file_name'] = $file_name;
            // end renaming
            $this->ci->upload->initialize($config);
            $this->ci->upload->do_upload($userfile);
            $newfile[]=$this->ci->upload->file_name;
       }
    }

    return $newfile;
}
Sign up to request clarification or add additional context in comments.

Comments

0

$this->upload->do_upload() expects a field name not the $_FILES array.

$this->upload->do_upload('images[parent]');
/*/
 * error handeling
/*/
$this->upload->do_upload('images[doctor]');
/*/
 * error handeling
/*/

this will upload 2 images

https://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html

Comments

0

Looping through uploaded files should work.

foreach($_FILES as $userfile){
   //some code here
}

Comments

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.