0

I'm to trying to upload multiple images in PHP CodeIgniter but it is displaying an error message as follows

Error Message: Illegal offset type in isset or empty
Filename: libraries/Upload.php
Line Number: 148
You did not select a file to upload.

My view file

<html>
    <head>
    <title>Upload Form</title>
    </head>
    <body>

    <?php
    if(isset($error))
    {
     echo $error;
    }
    ?>

    <?php echo form_open_multipart('upload/do_upload');?>

    <input type="file" name="userfile" size="20" />
    <input type="file" name="userfile1" size="20" />

    <br /><br />

    <input type="submit" value="upload" />

    </form>



    <?php 

    if(isset($upload_data))
    {
        echo "<ul>";

    foreach ($upload_data as $item => $value):

    echo"<li>";

     echo $item . $value;

     echo" </li>";

     endforeach;

    echo "</ul>";
    }
    ?>

    </body>
</html>

My controller file

<?php

class Upload extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
    }

    function index()
    {
        $this->load->view('upload_form', array('error' => ' ' ));
    }

    function do_upload()
    {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '10000';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';

        $this->load->library('upload', $config);

        $fname= array('f1' => 'userfile','f2'=>'userfile' );

        if ( ! $this->upload->do_upload($fname))
        {
            $error = array('error' => $this->upload->display_errors());

            $this->load->view('upload_form', $error);
        }
        else
        {
            $data = array('upload_data' => $this->upload->data());
            $this->load->view('upload_form', $data);
        }
    }
}
?>

Please help me out to get my problem solved.

4
  • i have try to re edite my question but it is not post complited so understand may question and help me Commented Dec 14, 2017 at 16:11
  • You have to loop through $_FILES to do multiple uploads in CodeIgniter Commented Dec 14, 2017 at 16:20
  • mins how i perform that Commented Dec 14, 2017 at 16:21
  • pleace give me solution i can not understand Commented Dec 14, 2017 at 16:26

1 Answer 1

2

So, in order to do multiple uploads in the same request, you change the way CodeIgniter sees the incoming uploaded files with something similar to this:

$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '10000';
$config['max_width']  = '1024';
$config['max_height']  = '768';

$this->load->library('upload');

$errors = [];
$files = $_FILES;
$upload_count = count( $_FILES['userfile']['name'] );
for( $i = 0; $i < $upload_count; $i++ )
{
    $_FILES['userfile'] = [
        'name'     => $files['userfile']['name'][$i],
        'type'     => $files['userfile']['type'][$i],
        'tmp_name' => $files['userfile']['tmp_name'][$i],
        'error'    => $files['userfile']['error'][$i],
        'size'     => $files['userfile']['size'][$i]
    ];

    $this->upload->initialize( $config );

    // Use upload library for file validation
    if( $this->upload->do_upload() )
    {
        // Upload was successful, do something ...
    }
    else
    {
        // Error: Upload Failed
        $errors[] = $this->upload->display_errors('','');
    }
}

This also means you change the names of your file inputs in the HTML to userfile[].

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

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.