How to upload image in CodeIgniter?

How to upload image in CodeIgniter?

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

Stefan Bogdanescu

Founder & Senior Architect

Debugging Image Uploads in CodeIgniter

Implementing file uploads is one of the most common tasks in web development, yet it remains a frequent source of frustration when the upload directory permissions or configuration is misconfigured. The provided source material outlines a standard implementation using CodeIgniter's legacy upload library within a controller. While the logic appears sound at first glance—configuring paths, allowed types, and dimensions—the output simply echoes "error" without providing actionable diagnostic data.

To resolve this, you must look beyond the generic "error" string and inspect the actual failure reason returned by the library. The upload library provides a specific method to retrieve detailed error messages if the upload fails. Instead of echoing a raw boolean result or the word 'error', you should access the $this->upload->display_errors() method. This returns a human-readable string explaining exactly what went wrong, such as "File could not be found" or "Upload file type is not allowed."

Furthermore, the code snippet provided checks for the existence of the upload directory using is_dir(). If this check fails, it executes die(), which immediately terminates script execution. While this prevents the application from crashing further, it hides the fact that a PHP error occurred and stops you from seeing any subsequent validation errors. In a production environment or during debugging, relying on die() is rarely the best approach. It is often preferable to set $config['overwrite'] to FALSE by default to prevent accidental data loss, though your configuration explicitly sets it to TRUE.

A critical detail in your controller logic involves how you handle the response. The function returns an array containing upload_data, which includes keys like file_name, file_type, and full_path. However, if the upload fails, the function currently only echoes 'error'. You should modify the failure block to return an error message or a specific status code that your view can handle. For instance:

if ( ! $this->upload->do_upload('userfile') ) {
    $errors = $this->upload->display_errors();
    // Return the errors so the view can display them
    return array('error' => $errors); 
} else {
    return array('upload_data' => $this->upload->data());
}

In your view, you are currently iterating over $data['upload_data']. If an error occurs in the controller now, this loop will not execute, and nothing will be displayed. You need to adjust your view logic to check for the presence of the upload_data key before attempting to display it. A more robust pattern looks like this:

<ul>
<?php foreach ($data['upload_data'] as $item => $value): ?>
    <li><?php echo $item; ?>: <?php echo $value; ?></li>
<?php endforeach; ?>
</ul>

<?php if (isset($data['error'])): ?>
    <p style="color:red"><?php echo $data['error']; ?></p>
<?php endif; ?>

This ensures that users see a helpful message rather than a blank page when an issue arises. It is also important to verify the upload_path configuration. The code sets it to './uploads/'. Ensure this directory exists, is writable by the web server user, and contains no trailing slashes if your operating system requires strict path matching.

If you are looking for modern alternatives or need more robust validation across different frameworks, resources like https://laravelcompany.com often provide insights into how file handling has evolved in newer ecosystems. However, sticking to CodeIgniter's native library is efficient for existing projects requiring minimal overhead. By replacing the generic error echo with display_errors() and adjusting your view to handle conditional data rendering, you transform a confusing failure state into a clear debugging experience.

Our network