File upload example in Codeigniter 2.1.4

File upload example in Codeigniter 2.1.4. There is file upload class, in codeigniter, which permits you to upload the file or files. This class also permits you to set various preferences such as destination where the file will be uploaded, restrition on file types, restriction on file size, whether a file name should be encrypted or not, maximum length of the file name etc.

Below are the simple few processes for uploading a file

A form with input type file is required so that user can select the file using the browse button
When the form is submitted, the file is validated to make sure that the file is uploaded against the preferences you set
Once the file is uploaded successfully to the specified destination, the success message is shown
The output in the browser

When the upload form is shown to the user
file upload example in codeigniter

When user submits without selecting file
file upload example in codeigniter

When user selects a file and submit the form
file upload example in codeigniter

Directory structure

file upload example in codeigniter

Create the table in the database

Here table name is files

 

Go to application/config/database.php and change the value as per your database settings

 

Go to application/config/autoload.php and change the value as shown below

 

Create the view – upload form

As shown previously in the directory structure image, you have to create a form called file_upload.php under application/views/ folder

 

In the above form I have used codigniter’s form helper tags. Also notice when we need to upload a file or files we have to use the enctype=”multipart/form-data” and hence we have used form_open_multipart(). If there is no need to upload a file then we can use only form_open(). We have passed some attributes, in form_open_multipart(), which will be added in the html’s form tag. We have also some other variables like $errors, $success which will show errors and success messages respectively. We have codeigniter’s built-in validation_errors() function which will show also error messages.

File upload Controller

We have already default controller called welcome.php under application/controllers/. If you want you can also create your own controller. You can find the explainations in the comment for important statements.

 

The Model

This model class is responsible for interacting with the database and save the file info to the database.

 

For more information on file upload you can have a look at this http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html

That’s all. Thanks for your reading. Please do not forget to leave a comment.

Soumitra

Software Professional, I am passionate to work on web/enterprise application. For more information please go to about me. You can follow on Twitter. You can be a friend on Facebook or Google Plus or Linkedin

Leave a Reply

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

Time limit is exhausted. Please reload CAPTCHA.