×

CodeIgniter File Uploading Class

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:

$this -> load-> library ( ‘upload’ );

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:

$config[ ‘upload_path’ ] = ‘./uploads/’;
 $config[ ‘allowed_types’ ] = ‘gif | jpg | png | mp3 |pdf’ ;
 $config[ ‘max_size’ ] = ‘200’;
 $config[ ‘max_width’ ] = ‘1024’; 
 $config[ ‘max_height’ ] = ‘820’;
 $this->load->library( ‘upload’, $config);  

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

Syntax

initialize ( [ array $config = array() [, $reset = TRUE ] ]);

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

do_upload ( [ $field = ‘userfile_name’ ] );

$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

display_errors ( [ $open = ‘<p>’ [, $close = ‘</p>’ ] ] );

$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 ($data);

$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

<?php
 class Online extends CI_Controller
 {   
     public function my_upload_form()
     {  
         $this->load->helper('form');
         $this->load->helper('url');
     $this->load->view('my_upload_view');
     }
     public function upload_my_file()
     {  
     $config = Array(
         'upload_path' => './uploads/',         // define the allocated folder to store the files
         'allowed_types' => 'png|jpg|doc|pdf|mp3|mp4 | gif', // define the type of file
         'max_size' => 11096,
         'max_width' => 2000,
         'max_height_' => 2000,
         );
         $this->load->library('upload', $config);         // load upload library and config library
     if (  $this->upload->do_upload('file_upload'))
     {
             $data = $this->upload->data();
               $this->load->view('success');  // load view file
     }
     else
     {    $error = $this->upload->display_errors();
         print_r($error);
         }
 }
 ?> 

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

<html>
 <head> 
 <title>Upload Form</title> 
 <body> 
 <form action ="<?php echo site_url('/online/upload_my_file'); ?>" method ="post" enctype="multipart/form-data" >
 <label> Select file to upload</label>
 <input type ="file" name ="file_upload"/> 
 <input type="submit" name="mybutton" value="Upload-file">
 </form>
 </body>
 </html> 

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

 <h2> File Uploaded successfully</h2>
   <br /> 
  <a href="http://localhost/codeIgniter-3.1.11/index.php/online/my_upload_form"> Upload more </a> 

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

Related Topics

Excel VBA Timer Function

Excel VBA Timer Function: The Timer function in VBA returns a Single data type, evaluating the number of seconds that have elapsed since midnight of the current day. Syntax Timer () Parameter NA Return This...

1 minute read.

Excel VBA Day Function

The function Day in VBA returns the day number (from 1 to 31) for the given date value. Syntax Day (Date) Parameter Date (required) – This parameter represents the date. Return This function returns the day...

1 minute read.

How to save Macro Workbook?

Saving Macro Workbook Default Excel File Extension The default excel file extension is “.xlsx”. But the standard file extension “.xlsx” cannot contains macros. So, the workbook contains macro when save din.xlsx file, all VBA...

1 minute read.

ActiveX Controls

ActiveX Controls are one of the most used Excel Controls to automate applications with Excel VBA. It has the same controls, unlike Form Controls (Command Button, combo box, checkbox, etc.), but it...

3 minutes read.

VBA Pivot Table

What is a Pivot Table? One of the most powerful features of Excel VBA is the Pivot Table. A pivot table is a VBA tool that is used to create summary...

3 minutes read.

VBA Type Mismatch Error

What is a Type Mismatch Error? VBA Type Mismatch Error is a run time error in excel, which often occurs when the data types contained in a VBA code are not matched...

3 minutes read.

Debugging in Excel VBA

Debugging in VBA: Debugging is a technique used to fix errors in programming languages. In Excel VBA, we have different ways by which you can identify the error in the...

2 minutes read.

VBA Color Index Property

What is Color Index Property? The Excel VBA Color Index is used to change the color for the cell or range of cells or text (located under the Font section). It sets the color...

5 minutes read.

VBA TimeSerial Function

The TimeSerial function in VBA returns a Time for the specified hour, minute, and second. Syntax TimeSerial (Hour, Minute, Second) Parameter Hour (required) – This parameter represents an integer (0 to 23), signifying the hour of the time. Minute...

2 minutes read.

Declaring a Variable in VBA

Declaring a Variable A variable is broadly described as a storage location combined with a name and representing a specific value. Declaring a variable is instructing the computer to reserve space...

2 minutes read.

Steps to Create a Pivot Table

Steps to Create a Pivot Table: Pivot Tables can be easily automated through VBA coding. To create a Pivot Table in VBA, follow the below step by step procedure to...

4 minutes read.

Excel VBA CVErr Function

VBA CVErr Function: The CVErr function in VBA returns an Error data type, involving with a user-specified error code. Syntax CVErr (Expression) Parameter Expression (required)- This parameter represents the required error code. Return This function returns an...

1 minute read.

Excel VBA IsDate Function

VBA IsDate Function: The IsDate function in VBA returns a Boolean value indicating whether the given expression is interpreted as a VBA Date or not. Syntax IsDate (Expression) Parameter Expression (required) – This parameter...

1 minute read.

Excel VBA Hex Function

VBA Hex Function: The Hex function in VBA converts the given number into hexadecimal notation and returns the result as a string. Syntax Hex (Number) Parameter Number (required) – This parameter represents the numeric value...

1 minute read.

Excel VBA AutoFilter

One of the reasons for Excel VBA’s popularity is its capability to filter and analyze data from huge database with the help of a method known as AutoFilter. This method permits a...

4 minutes read.

Miscellaneous Exercise of conditional statements and Loop

We have the already worked with syntax and examples of conditional statements and loops in the previous tutorials. In this tutorial, we will learn how to work with both together.  We will explain...

3 minutes read.

Excel VBA IsError Function

VBA IsError Function: The IsError function in VBA returns a Boolean value showing whether the specified Expression represents an error or not. Syntax IsError (Expression) Parameter Expression (required)- This parameter represents the variant that you...

1 minute read.

Excel VBA InputBox

Input Box The InputBox function in VBA is used to prompt the users to enter values. The user can click either the OK button or can choose the CANCEL button. If the user clicks...

3 minutes read.

Excel VBA CByte Function

VBA CByte Function: The CByte function in VBA converts an expression into a Byte data type. Syntax CByte (Expression) Parameter Expression (required) - This parameter represents the expression that that you want to convert to...

1 minute read.

CodeIgniter File Uploading Class

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...

5 minutes read.