Upload File in CodeIgniter Framework

0 98

This tutorial will show how to upload file in CodeIgniter using File Uploading Class. CodeIgniter’s File Uploading Class helps to upload files to the server. You can use various preferences for configuring your file upload and restrict the type and size of the files.

For your better understand we’ll demonstrate the file upload with an example. Also, we’ll upload the user picture to the server and insert the user data to the database.

Creating Database Table (users):

Before start to writing the code, create a table into your application’s database. For this example you can create a users table into the database with some basic fields. The users table SQL would like the below.

Controller (Users.php):

Create a controller called Users.php. Place the following code in it and save this file to application/controllers/ directory.
User model loaded into the __construct() function for inserting user data into the database.
Initially, add() function will load the user adding form. When the form is submitted, picture would be uploaded and data would be inserted into the database via User model.

We’ve used some required Preferences in our file upload configuration. You can get all the available Preferences from here – https://codeigniter.com/user_guide/libraries/file_uploading.html#preferences

Model (User.php):

Create a model called User.php. Place the following code in it and save this file to application/models/ directory.
insert() function insert the provided data into the users table.

View (add.php):

Create a view called add.php. Place the following code in it and save this file to application/views/users/ directory.
add.php file contains form HTML, it is used to collect the user data and submit to the controller.

Creating Upload Directory:

Create the uploads/images/ directory to storing the uploaded images.

You might also like More from author

Leave A Reply

Your email address will not be published.