6 Steps to Create a File Upload System Script in CodeIgniter
  • Home
  • PHP

6 Steps to Create a File Upload System Script in CodeIgniter

File Upload System Script in CodeIgniter, In this tutorial you are going to learn about creating file upload system in CodeIgniter. Previously, I’ve created a crud application in CodeIgniter, You can go through the tutorial. In this file upload system, I’m going to upload a file using CodeIgniter and inserting it into the database. Displaying all the uploaded files and deleting uploaded files. These are the functions we are going to build in this tutorial.

=> Creating File Upload System Script in PHP & MySQL
=> Create CRUD Application in CodeIgniter

1. Download CodeIgniter & Setup

First of all download Code Igniter from codeigniter.com website, I’m using Code Igniter 3.1.5 for this CRUD application. This is the latest version available. Extract this package, place these files on your server. Update htaccess file with this below code to remove index.php from URL, By default the index.php file, will be included in your URLs

Open config.php file from application->config->config.php and update base URL with the path where you have uploaded. In my case I’ve uploaded to http://localhost/ciupload/, in your case it may be different.

Remove index.php from below line of code and make it blank.
From this code

To this code

2. Create Database & Table

We are going to work on application directory. In this directory, we will create all the necessary files for models views and controllers. Next, we need to configure the database in CodeIgniter.

Before that create database and database table, below is the SQL code of database table.

3. Authenticate in CodeIgniter

In database.php file update the database user name, password & database name. You can find database.php file in application-> config-> database.php

4. Creating File Upload Form View

Then I’m going to create a view for our File Upload form. Before that I’ll create a directory under views with the name of fileupload. Inside this fileupload directory, I’m going to create new php file and save it as upload.php.

In this upload.php file, I’m going to add basic html with bootstrap cdn files and the form.

Adding bootstrap CDN files

Adding form inside upload.php file within body section

5. Creating Controller with Index Method & Constructor

After creating the view, I’m going to create a controller to display the form and send submitted values to model. I’ll create both controller methods. One with the name of index and the another one with the name of do_upload.

Before creating index method. I’ll create a constructor to load form helper and url helper, also to load model I’ll name it as upload_model which will be used in next steps. Create a file under controllers directory and save it as upload.php. Then add this below code.

Then I’m creating Index method in our controller file.

If you open the url in browser you can see the form your-file-path/upload/index

6. File Upload Method in Controller

Next, I’m going to create a controller method with the name of do_upload. First I’ll define the configuration variables, then I’m going to load file upload library with the configuration options defined. After that, I’m going to write an if condition to check file upload. If there are any errors we will display the errors and then sends these errors to upload index view.

If there are no errors I’ll upload the file to the server and insert the record into our upload database table.

7. Creating Model to Insert Data & Constructor

Next we need a model to insert submitted data into database. For that I’m going to create a model with constructor that loads database.

Next is an insert query to insert the data into database.

Add this line of code in the else block of do_upload controller method

8. Creating View for Displaying Errors

Add this below code in upload.php file under views. This line should be above form opening tag.

9. Creating View to Display all the uploaded files

I’m creating a view for displaying uploaded files with a table.

10. Add View Method in Controller

Creating View Method in Controller to display our view to list all the records.

11. Fetching records from Database using Model

Selecting all the records from uploads table using get_uploads method from upload model.

12. Displaying Records the View

Displaying Records from database using model method.

13. Creating Delete Method in Controller

Final step is creating Delete method in Controller

14. Add Delete Model to Delete Uploaded File

I’m creating a delete_upload method to delete record from database and also to delte file from controller.

If you are getting any problem let me know through comment form below.

Vivek Vengala

Vivek Vengala is a Online Entrepreneur, Web Developer from Hyderabad India.