Multiple image upload with CodeIgniter

Multiple image upload with CodeIgniter

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Fixing Multiple File Uploads in CodeIgniter

When working with multiple file uploads in CodeIgniter, a common pitfall occurs when all input fields share the same name attribute. In the scenario provided, three distinct <input type="file"> elements are all named userfile. While HTML5 allows browsers to send an array of files under that single name via $_FILES['userfile'], CodeIgniter's default $this->upload->do_upload('userfile') method only processes the first file in that array.

Consequently, the upload library returns data for a single file object, and your logic attempts to access keys like prod_image1 and prod_image2 which do not exist in the returned $data array. This causes those variables to remain empty or default to NULL, while only the first image gets processed. To store three separate images in your database columns (prod_image, prod_image1, prod_image2), you must either give each input field a unique name or configure the upload library to handle multiple files explicitly.

Unique Input Names for Multiple Uploads

The most straightforward approach for legacy CodeIgniter versions (pre-3.1) is to assign unique names to each file input. This ensures that $_FILES contains separate keys like userfile_1, userfile_2, and userfile_3.

Update your view as follows:

<input class="form-control" name="pd_name" type="text" />
<input type="file" class="file_upload2" name="userfile_1" />
<input type="file" class="file_upload2" name="userfile_2" />
<input type="file" class="file_upload2" name="userfile_3" />

In your controller, you must call do_upload three times, once for each field. The method returns the upload data for that specific field only. You need to handle the success status and retrieve the file name individually for each input.

function products()
{
    date_default_timezone_set("Asia/Kolkata");
    
    $config['upload_path'] = './resources/images/products/';
    $config['allowed_types']        = 'gif|jpg|png';
    $config['max_size']             = 1000;
    $config['max_width']            = 1024;
    $config['max_height']           = 768;
    
    $this->load->library('upload', $config);

    // Initialize arrays to hold file names
    $images = array(
        'prod_image' => NULL,
        'prod_image1' => NULL,
        'prod_image2' => NULL
    );

    // Upload for the first field
    if (empty($this->upload->do_upload('userfile_1'))) {
        $error = array('error' => $this->upload->display_errors());
        return $error;
    } else {
        $data = $this->upload->data();
        $images['prod_image'] = $data['file_name'];
    }

    // Upload for the second field
    if (empty($this->upload->do_upload('userfile_2'))) {
        $error = array('error' => $this->upload->display_errors());
        return $error;
    } else {
        $data = $this->upload->data();
        $images['prod_image1'] = $data['file_name'];
    }

    // Upload for the third field
    if (empty($this->upload->do_upload('userfile_3'))) {
        $error = array('error' => $this->upload->display_errors());
        return $error;
    } else {
        $data = $this->upload->data();
        $images['prod_image2'] = $data['file_name'];
    }

    // Prepare insert data
    $insert_data = array(
        'name' => $this->input->post('pd_name'),
        'prod_image' => $images['prod_image'],
        'prod_image1' => $images['prod_image1'],
        'prod_image2' => $images['prod_image2'],
        'created_time' => date('Y-m-d H:i:s')
    );

    // Insert into database
    $this->tbl_products_model->insertUser($insert_data);
}

Handling Multiple Files via Arrays

If you prefer to keep all input fields named userfile, you must configure the upload library to recognize that multiple files are being sent. In CodeIgniter 3.x, this is done by setting the $config['max_files'] property.

Update your configuration:

$config['upload_path'] = './resources/images/products/';
$config['allowed_types']        = 'gif|jpg|png';
$config['max_size']             = 1000;
$config['max_width']            = 1024;
$config['max_height']           = 768;
$config['max_files']            = 3; // Allow up to 3 files under 'userfile'

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

With this configuration, do_upload('userfile') returns an array of file data if multiple files are uploaded successfully. You can then iterate through the results or access them by index. However, mapping specific indices (0, 1, 2) to specific database columns (prod_image, prod_image1, prod_image2) requires careful validation. For instance, if a user uploads only one file but expects three columns, you must decide whether to leave the other columns NULL or reject the entire upload.

For the array approach, your controller logic would look like this:

$config['max_files'] = 3;
$this->load->library('upload', $config);
$data = $this->upload->do_upload('userfile');

if (!$data) {
    die(print_r($this->upload->display_errors(), TRUE));
}

$files = (array) $data; // Ensure it is treated as an array

// Map first three files to columns if they exist
$images['prod_image'] = isset($files[0]) ? $files[0]['file_name'] : NULL;
$images['prod_image1'] = isset($files[1]) ? $files[1]['file_name'] : NULL;
$images['prod_image2'] = isset($files[2]) ? $files[2]['file_name'] : NULL;

// Proceed with database insertion...

Trade-offs and Best Practices

Using unique names (userfile_1, userfile_2) offers better control over validation. You can validate each file independently (e.g., ensuring all three are present) before proceeding. The array method simplifies the controller code but relies on the order of uploads, which might be ambiguous if the user skips the first field.

Additionally, consider using a database column type that supports NULL values for optional image fields. If your requirement is strictly "three images must exist," you should enforce this in the model or controller logic before attempting the INSERT operation. For modern projects migrating to newer stacks, frameworks like Laravel offer more streamlined multiple upload handling via request()->file('userfile') which automatically resolves arrays, but in CodeIgniter, explicit handling remains necessary for clarity and control.

Our network