Codeigniter 3 – Upload file and insert data into database!

NOTE: This tutorial requires the basic knowledge of Codeigniter to understand it.

In this post we are going to see how to upload a file using Codeigniter 3 framework and save the uploaded file data into the database.

I have used Codeigniter 3.4.1 for this demo. Also I am going to attach this demo script at the end of the post so you can download and play with the code.

If you want to start from scratch, just go to the Codeigniter website and download the latest version yourself, here is the link: https://codeigniter.com/download

After you download the zip, extract it and put it inside the htdocs folder (local environment). Then rename it as you wish but if you want to follow step by step rename it to ‘codeigniter3‘. So the folder structure may look something like this c:/xampp/htdocs/codeigniter3

MYSQL PART:

Now go to MySQL or open phpMyadmin and create a database name called ‘codeigniter3‘ and then create a table called ‘pictures’. I am attaching the table structure here:

MySQL part is over. No need to go to the database again.

HTACCESS:

Create .htaccess file inside codeigniter3 folder and put the following code in it:

This removes the index.php from the URL

 

CODEIGNITER CONFIGURATIONS:

1) First, open the database.php file inside /codigniter3/application/config/database.php and change the mysql hostname, username and password as per your database server configuration:

2) Then, open config.php file inside /codigniter3/application/config/config.php and set the $config[‘base_url’] to $config[‘base_url’] = ‘http://localhost/codeigniter3/’;

3) And finally our demo requires some libraries and helpers should be auto-loaded. Especially database library, form and url helpers. So open autoload.php  inside /codigniter3/application/config/autoload.php and then find the below variable $autoload[‘libraries’] and $autoload[‘helpers’] and change it as shown here:

Now everything is set. Let’s create our Models, Views and Controllers.

MODELS:

We are going to create only one model for this demo – Pic_model.php.

Code for Pic_model.php (PATH:  /codeigniter3/application/models/)

The above model has only two function in it. get_all_pics() which will fetch all the results from the table ‘pictures’ and store_pic_data() will insert the data into the same table ‘pictures’.


CONTROLLERS:

There are two controllers we are going to write for our demo script. Welcome.php and Upload.php

Code for Welcome.php (PATH:  /codeigniter3/application/controllers/):

Welcome.php is the default controller which is going to load as soon as you type http://localhost/codeigniter3. This controller fetches the picture list from database ($this->pic_model->get_all_pics())and then loads the data to the picture_list view.


Code for Upload.php (PATH:  /codeigniter3/application/controllers/):

Upload.php – This controller  validates the form and does the upload job. Just read through the inline comments in the above code to understand it.


VIEWS:

There are four views we have to create for this demo. In that two files are just partials (header.php and footer.php) and the other two files are main files (picture_list.php and upload_form.php)

Code for header.php (PATH:  /codeigniter3/application/views/):


Code for footer.php (PATH:  /codeigniter3/application/views/):


Code for picture_list.php (PATH:  /codeigniter3/application/views/):


Code for upload_form.php (PATH:  /codeigniter3/application/views/):

You can see the live demo of this code or just download the entire script here:

demodownload

 

 

 

2 Comments

Add a Comment

Your email address will not be published. Required fields are marked *