Example – Upload Multiple Files and Images in CodeIgniter Framework

0 70

CodeIgniter’s File Uploading Class allows uploading files on the server. You can upload single file or image easily using Upload library. But sometimes multiple file uploading feature needs to be provided the in the web application. This article will help to upload multiple file and image at once using CodeIgniter’s Upload Library. Also, we’ll show you how to insert the uploaded file information into the database.

Database Table Creation

Create a table called files to store the file information in the database (codeigniter_db). The table creation SQL would something like the below.

Upload Folder Creation

Create a directory on the server where you want to store the uploaded files. For example, create a uploads/files/ directory in the root folder of the application.

Controller (Upload_files.php)

__construct() : In this function, we’ll load the File model which will help to get file information from the database and insert files information in the database.

index() : This function displays all the images which are already uploaded. On submitting the upload form, files would be uploaded to the specified folder using Upload library and insert the files data into the database using File model.

We’ve set basic preferences in Upload library ($config), you can set various preferences provided by the Upload library. Complete preferences list can get from here – https://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html

Model (File.php)

File model consists of two functions getRows() and insert(). getRows() function is used to fetch the file information from the database and return it as an array. insert() function used to insert the provided file information into the database.

View (upload_files/index.php)

In the view file, we’ll display the HTML upload form with file input and a submit button. Under the file uploading form, all the uploaded images would be displayed.

Config (autoload.php)

You need to load some system library and helper for this example script. Open the config/autoload.php file and specify the library (session,database) and helper (url) which are used in the application.

By accessing the index method of the Upload_Files controller (http://localhost/codeigniter/upload_files) on the browser, file uploading form and uploaded files


In this example script, we demonstrate the multiple images upload process. However, you can use this script to upload all types of file. You only need to set the allowed_types preference in Upload library. To accept all types of file upload set the following preference in CodeIgniter File Upload class.

You might also like More from author

Leave A Reply

Your email address will not be published.