Chat with us, powered by LiveChat

This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

Say hello to redesigned Cloudways, an empowering Startup Program, enhanced Staging, a new Let’s Encrypt Wildcard SSL certificate feature, and more. GET STARTED

How to Upload Image and File in CodeIgniter

Updated on  28th July

3 Min Read
Reading Time: 3 minutes

File uploads are an essential process in many web apps. Besides having Best PHP Hosting, almost every website and web app requires an integrated file upload component. File and image upload in CodeIgniter powered app is a simple component that takes care of the upload process with little issues.

image and file upload in codeigniter

This tutorial discusses the process of creating a CodeIgniter based file upload component that could be easily used t upload images and other files with ease. You could validate and even restrict file size and type during the upload process.

Folder Creation

The first step is the creation of two folders that form the basis of the upload process. The first folder is the destination folder that would receive the uploaded files.

Next, go to the root of the CI installation and create a folder named “uploads”.

Go to the view folder (located at the root of the CI installation)  and create two new “view” files. Name these files file_view.php (displays the form containing file upload fields) and upload_success.php (displays the upload successful message).

Related: How To Host CodeIgniter PHP On Cloud

Create the Controller

The next step is the creation of a file in the controller folder.  Name the file upload_controller.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:

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

you could see this function in action when you call the file_view.php file. if you are at a staging domain, use the URL: your-domain/ci_demo/index.php/upload_controller/file_view OR in the case of loclhost: localhost/ci_demo/index.php/upload_controller/file_view

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:

You Might Also Like: Simple Guide to CodeIgniter Form Validation

Structural View

For the structure of the image upload, I will start by creating a file in the views folder  with the name custom_view.php. Open this file and add the following code to it:

Next, go to the controller folder and create a new file with the name upload_controller.php

You might also like: How To Pass Data From Controller To View In CodeIgniter

Add the following code to this file:

Notice that the array $config of the do_upload()  has an element named allowed_types. you could use the parameters of this element to change the allowed file types. For example, for allowing a range of file, use the following structure of this element:

The Success Message

To display the upload successful message, I will use the upload_success.php file (located in view folder). Add the following code to it:

Note: Make sure that you are calling the correct path for the controller class

Related: How To Create A REST API In CodeIgniter


This was a short tutorial on creating an image and file upload process in a CodeIgniter powered application. remember that you could easily modify the allowed file types and image parameters through the code.

If you need help in understanding the code or integrating the code into your CI application, do leave a comment below.

Share your opinion in the comment section. COMMENT NOW

Owais Alam

is the WordPress Community Manager at Cloudways - A Managed WooCommerce Hosting Platform and a seasoned PHP developer. He loves to develop all sorts of websites on WordPress and is in love with WooCommerce in particular. You can email him at

Create PHP websites without the worry of server management.

Deploy your PHP app on optimized PHP hosting servers.

Get Our Newsletter
Be the first to get the latest updates and tutorials.