Google trends Public Holidays Coupon Code Code Compiler

How to Upload File and Image in CodeIgniter

Feb 6, 2019

File uploads are an essential process in webbase application. Almost every website and web app requires an integrated file or image upload component. File and image upload in CodeIgniter. This tutorial discusses the process of creating a CodeIgniter based file upload that could be easily used upload images and other files with ease. You could validate and even restrict type of file during the upload process.

go to the root of the CI installation and create a folder named “upload”.

Go to the view folder (located at the root of the CI installation) and create a new “file_upload.php” file.

Create a file in the Controller folder

Now the next step is the creation of a php file in the controller folder. Name the file uploadController.php. In this file, I will load a library for initializing the Upload class through the following code:


Set File Upload Preferences

I will also set the preferences for the file upload process through the controller function do_upload(). this function will contain the following code:

$config['upload_path'] = 'upload';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['remove_spaces'] = FALSE;

As you could see, through this function, you could easily restrict the upload path, allowed file types and dimensions of the image.

The Form Helper

Note that file upload requires a multipart form. For this, I have included a form helper in the controller with the following syntax:

$this->load->helper(array('form', 'url'));

Now open “file_upload.php" file from view folder and add the following code to it:

 <title>Image or file upload in codeigniter</title>
  <form action="<?php echo base_url('uploadController/upload_image') ?>" method="post" enctype="multipart/form-data">
    <label>Upload image:</label>
    <input type="file"  name="img" />
    <button type="submit" id="submit" name="submit">Upload</button>

Now open uploadController.php" file from controller folder and add the following code to it:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class uploadController extends CI_Controller {
public function __construct() {
$this->load->helper(array('form', 'url'));
public function index(){
public function upload_image($filename){
	if ($_FILES['image']['name'] != "") {
		$filename = $_FILES['image']['name'];
		$result = $this->do_upload($filename);
		echo 'File uploaded successfully.';
		echo 'Error in file upload.';
public function do_upload($filename){

	$config['upload_path'] = 'assets_signi/upload/blog/';
	$config['file_name'] = $filename;
	$config['allowed_types'] = 'gif|jpg|png|jpeg';
	$config['remove_spaces'] = FALSE;

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

	if (!$this->upload->do_upload('file')) {
		return FALSE;
	} else {
		return TRUE;

Copyright 2024. All rights are reserved