×

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 Minute Function

The Minute function in VBA returns the minute component for the specified time. Syntax Minute (Time) Parameter Time (required) – This parameter represents the time. Return This function returns the minute component for the specified time. Example 1 Sub MinteFunction_Example1() ...

1 minute read.

Excel VBA MonthName Function

Excel VBA MonthName Function: The MonthName function in VBA returns a string with the month name for the specified month number. Syntax MonthName (Month, [Abbreviate]) Parameter Month (required) – This parameter represents an integer between...

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 VBScript Regex Methods Regex

VBA VBScript Regex Methods Regex The VBA Regex supports 3 methods which are as follows: ExecuteReplaceText Execute The execute method is used to extract a match from the given based on the defined matching...

3 minutes read.

Excel VBA Round Function

VBA Round Function: The Round function in VBA rounds a number to a specified number of decimal places and returns the number. Syntax Round (Number, [NumDigitsAfterDecimal]) Parameter Number (required) –This parameter represents a numeric value one wants...

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.

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.

VBA Dim

What is Dim? DIM or Dimension or Declare in Memory is a keyword that is used in VBA to declare a variable with the different data types (Integer, String, variable, Boolean, Double, etc.)...

7 minutes read.

Excel VBA Error Function

VBA Error Function: The Error function in VBA returns the error message corresponding to a supplied error code. Syntax Error ([ErrorNumber]) Parameter ErrorNumber (optional) – This parameter represents the required error number. By default, the...

1 minute read.

Excel VBA For Each Loop

A For Each loop executes a statement or a group of statements for each element in an array or collection. It repeats the statement/condition/code for each element in a collection. For Each Loops loop through every...

4 minutes read.

Looping in VBA

Looping in VBA There are many situations where a programmer needs to execute a block of the repetitive code number of times. Writing the same statement will make the program tedious and monotonous....

3 minutes read.

Excel VBA Fix Function

VBA Fix Function: The Fix function in VBA truncates the given number to an integer and returns the rounded off integer number. This function both positive and negative numbers to zero. Syntax Fix...

1 minute read.

Excel VBA InStrRev Function

VBA InStrRev Function: The InStrRev function in Excel VBA returns an integer representing the position of a substring within the specified string if the substring is fount else it returns...

2 minutes read.

Excel VBA While wend Loop

WHILE wend loop is used when the user is not sure how many times they want to execute the VBA code within the program. With a WHILE loop, the loop body may...

3 minutes read.

Excel VBA Sin Function

VBA Sin Function: The Sin function in VBA returns the sine value for a supplied angle. Syntax Sin (Number) Parameter Number (required) –This parameter represents the angle (in radians) to calculate the sine value. Return This function...

1 minute read.

VBA Regex

What is a Regex? Regex stands for Regular Expression is basically a pattern matching strings within another string. They are supported in many languages, including .net, C++, Python, etc. They are...

5 minutes read.

Excel VBA LBound Function

Excel VBA LBound Function: The LBound function in VBA returns the lowest subscript for the specified dimension in the given array. Syntax LBound (ArrayName, [Dimension]) Parameter ArrayName (required) – This parameter represents an array for which...

1 minute read.

Scope in Visual Basics

Definition of Scope The scope of any programming language implies the area of code where the variables will be identified, accessed, and used. Every variable has a scope associated with it. The scope of...

4 minutes read.

VBA Global Variable

What is Global Variable? The Global Variables in VBA refers to the variables declared before the start of any macro. They are defined outside the functions and are used by all the functions or...

6 minutes read.

Excel VBA Array Function

VBA Array Function: The Array function in VBA generates an array containing the given set of values. Syntax Array (Arglist) Parameter Arglist (required) – This parameter the list of values that you want to make...

2 minutes read.