CodeIgniter File Uploading Class

Facebooktwitterredditpinterestlinkedinmailby feather

The Codeigniter provides a file Uploading library class which is used to upload any file such as images, pdf, mp3, etc. to the codeigniter’s application. It also allows to set various preferences such as the type and size of files.  

Load the File Uploading Class

Before using the file uploading class in Codeigniter, you must load in the controller file by following the

Syntax:

These are the following methods that are available in the file uploading library to upload the documents.

1. initialize(): An initialize () function is used to initialize or load the config preference that is set in the config file. It also allows to set the preference manually in the controller file such as:

Alternatively, you can set file preferences by calling the initialize() method to auto-load the config class.

Syntax

It has two parameters

$config (array): In this field, you can set the file related preferences such as size, width, type, etc.

$reset: In this parameter, you can set either TRUE or FALSE value that indicates whether you want to reset the default preferences of the config.php file.

2. do_upload(): It is used to define the name of the form field or filename that you want to upload to the application.

Syntax

$field: In this fields, you can pass the file name.

3. display_errors(): As the name represents, it is used to displays an error message, when the particular file is not uploaded to an application.

Syntax

$open: It defines an open markup to display the formatted error message.  

$close: It defines a close markup to display the formatted end of the error message.

Note: It throws an error message when the do_upload() function is unable to upload the file.

4. data(): The data() function contains all the information that relates to files uploaded in the application such as file_name, file_type, file_path, etc.

Syntax

$data: You can define an element instead of the full array.

Write a program of file Uploading in the CodeIgniter application:

Before creating a file uploading program, you have to create a folder uploads in Codeigniter application.

File Uploading Class

Create a controller file Online.php and save it in application/controller/Online.php. After that, write the following program in the controller file.

Online.php

Create a view file my_upload_view.php and save it in application/views/my_upload_view.php. After that, write the following program in the controller file.

my_upload_view.php

Create a view file success.php and save it in application/views/success.php. After that, write the following program in the controller file.

success.php

Now execute the program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/ Online/my_upload_form function, it shows the output, as shown below.

File Uploading Class

Now click on the Choose File button;it allows you to select a file from your system.

File Uploading Class

After selecting the file, click on the Upload-file button to upload and save the file in the Codeigniter application. When the file uploads successfully it shows the messaeg File Uploaded successfully, it saves the file in Uploads folder. And if you want to upload more files, click on the Upload more link. It redirects, you to the starting page “localhost/CodeIgniter-3.1.11/ Online/my_upload_form” of the upload file. 

File Uploading Class

And if you want to check, your files have been successfully uploaded, go to the CodeIgniter-3.1.11\uploads folder and it shows the below image of uploaded files.

File Uploading Class
Facebooktwitterredditpinterestlinkedinmailby feather