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

Codeonion contributes to the honorable mission of “helping people learn and earn online”. Whether your day job is creative or not, life is better when you learn and apply creative skills regularly. To do this is satisfying in its own right and a practical investment in your future.

Comments (8)

  1. Atleena says:

    Sir I want to add only one image at a time without any validation . please help me

    1. codeonion says:

      There are parameters you can set in the config or the controllers. Multiple files and single files can be set just like the file format.

  2. Atleena says:

    Hello sir, I am getting error like
    You did n’t select any file to upload

    1. codeonion says:

      Did you select a file 😀 ?

  3. Thanks, you are a life saver!

    1. codeonion says:

      You’re welcome! And thanks for the kind words and visiting my blog 🙂

      Let me know in future if you wish to have me write a tutorial that you will like.

  4. sdkfds says:

    that’s useless…. You upload to a directory that is accessible from http://www... nice try but it is dangerous.

    1. codeonion says:

      Add an index.php with “Directory access is not allowed!” in it and it will fix many problems.

      Also, you can put a .htaccess file for denying access to your folder 🙂

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.