×

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

VBA CLng Function: The CLng function in VBA converts an expression into a Long data type. It returns a long value ranging between -2,147,483,648 and 2,147,483,647. Syntax CLng (Expression) Parameter Expression (required) – This...

1 minute read.

Excel VBA Filter Function

VBA Filter Function: The Filter function in VBA returns a subset for the given string array, based on specified criteria. Syntax Filter (SourceArray, Match, [Include], [Compare]) Parameter SourceArray (required) – This parameter the array of Strings that you...

2 minutes read.

Excel VBA Space Function

VBA Space Function: The Space function in VBA creates a String consisting of a specified number of spaces. Syntax Space (Number) Parameter Number (required) - This parameter represents the number of spaces. Return This function returns a...

1 minute read.

Excel VBA Format Function

VBA Format Function The format function in VBA applies a specified format to an expression and returns the result as a string. Syntax Format (Expression, [Format], [FirstDayOfWeek] , [FirstWeekOfYear] ) Parameter Expression (required)- This parameter represents the expression that you want to format. Format...

3 minutes read.

Excel VBA Replace Function

VBA Replace Function: The Replace function in VBA searches for a substring within the specified string and replaces its occurrences with a second substring. Syntax Replace (Expression, Find, Replace, [Start], [Count], [Compare]) Parameter Expression (required) – This parameter represents...

2 minutes read.

Excel VBA Join Function

Excel VBA Join Function: The Join function in VBA is used to join an array of substrings and return them all as a single string. Syntax Join (SourceArray, [Delimiter]) Parameter SourceArray (required) – This parameter...

1 minute read.

Excel VBA ABS Function

VBA ABS Function: The ABS function in VBA returns the absolute value of the specified number. Syntax Abs (Number) Parameter Number (required) – This parameter represents the number that you want the absolute value of. Return This...

1 minute read.

Introduction to Visual Basic Editor Window

How to enable the Developer Ribbon Tab? In order to work with VBA, users need to make a small change in Excel to display a new tab (Developer) at the top of the...

5 minutes read.

Excel VBA Sgn Function

VBA Sgn Function: The Sgn function in VBA returns an integer (+1, 0, or -1), stating the arithmetic sign for the specified number. Syntax Sgn (Number) Parameter Number (required) –This parameter represents the number that...

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

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

VBA Val Function: The Val function in VBA converts the given string into a numeric value. This function ignores spaces and continues to read the characters after space(s). It stops...

1 minute read.

Excel VBA UBound Function

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

1 minute read.

Excel VBA FormatCurrency Function

VBA FormatCurrency Function: The FormatCurrency function in VBA is used to apply a currency format to a numeric expression and returns the result as a string. Syntax FormatCurrency (Expression, [NumDigitsAfterDecimal], [IncludeLeadingDigit], [UseParensForNegativeNumbers], [GroupDigits]) Parameter Expression (required)...

2 minutes read.

Excel VBA CDec Function

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

1 minute read.

Excel VBA FormatDateTime Function

VBA FormatDateTime Function: The FormatDateTime function in VBA returns the result as a string after applying a date and/or time format to the supplied expression. Syntax FormatDateTime (Expression, [NamedFormat]) Parameter Expression (specified) – This parameter...

2 minutes read.

Excel VBA StrReverse Function

The StrReverse function returns a String after reversing the given String. Syntax StrReverse (Expression) Parameter Expression (required) – This parameter represents the String that you want to reverse. Return This function returns a String after reversing the given String. Example...

1 minute read.

Excel VBA Len Function

VBA Len Function: The Len function in VBA returns the number of characters in a supplied string or the number of bytes required to store a supplied variable. Syntax Len (Expression) Parameter Expression (required)-...

1 minute read.

Excel VBA Time Function

Excel VBA Time Function: The Time function in VBA returns the current time. Syntax Time () Parameter NA Return This function returns the current time.  Example 1 Sub TimeFunction_Example1() ' returns the current time Dim time_val...

1 minute read.