Codeigniter 2.20 – Uploading files to a folder and Inserting in Database table

Back to CodeIgniter Tutorials

In this tutorial, I am going to show how to upload multiple files into a folder and insert its name in a database table. In an application, we may either have to upload a single or multiple files. Take freelancer.com portfolio page for instance. They allow freelancers to upload multiple files for the same portfolio item. So our goal here is the same. The goals are:

  1. Take one or multiple files as an input.
  2. Once submit is pressed, the file is uploaded.
  3. When the file is uploaded, an its name is inserted in the database table.

In order to understand this tutorial, you must know how to install, and use codeigniter. Follow the following links if you want to learn these steps.

Now lets begin!

In order to upload a file to a folder, first a view is built that takes File as an input and has a submit button. I am using a controller named c_upload and my view is v_uploader

VIEW: v_uploader

I am taking multiple files as an input.

If you wish to upload a single file, then all you need to do is to remove the “multiple” keyword in line 5. Here is the code.

 

CONTROLLER: c_upload

From view, I am calling a function called do_upload(). I also have another function set_upload_options().

Load these helpers

Load these libraries

Load this model

Finally, here are the functions

Note: In set_upload_options() function, you can configure upload path

Now the Model code

These are the steps explaining the program.

Back to CodeIgniter Tutorials