How To Upload Any Types Of File Using CodeIgniter

Here we are going to learn, How to upload files using CodeIgniter? File uploads are an essential process in many web apps. If we go for custom implementation, we need to do some basic implementation manually. Which makes code too hard, redundant, risky or may be chance of bugs. To resolve this issue CodeIgniter framework provides file upload library. So here we will learn to upload files using CodeIgniter library.

This tutorial discusses the process of creating a CodeIgniter based file upload component that could be easily used to upload any types file. You can validate and the file size and types at the time of upload.

1. Create Upload Folder

First step is to create “Upload” folder which is the destination folder to receive uploaded file. So now go to CI installation and create a folder named “uploads”.

2. Create The View File

Next step is create view file to display file upload form. Go to the view folder located at root of the CI installation and create file named “file_view.php”. Add the following lines of code inside the “file_view.php” file.



3. Create The Controller

The next part is create the file inside Controllers. This is the main part for the file upload. Go to the “controllers” folder inside the CI installation and create file named “upload_controller.php”. Inside the upload_controller.php first we need to initialize Upload class of CodeIngniter library by using following code of line.

To set upload file preferences you need to add following lines of code inside your function.


If you want to upload selected file types then you need to specify perticular file types using “allowed_types” attributes. For example you want to upload only images on the server then add following line.

Add following code to upload all types of file inside the upload_controller.php.



Note : Notice that the array $config of the insert() has an element named allowed_types. You can change the file types as per your requirements. For example, for allowing a range of file, use the following structure of this element:


4. Conclusion

This was the short code to easily upload any types of file using CodeIgniter. If you have any further quary or any problem regarding this code please write the comment below.

Happy Coding 🙂