CodeIgniter Number helper

The number helper file contains some predefined function that deals with numeric data to display numbers in bytes format.

Load a Number Helper

You must load a number helper file in the controller to perform a number function in the CodeIgniter application.

Syntax

$this->load->helper (‘number’);

Function of Number Helper:

  1. byte_format(): As the name suggests, it is used to convert a large number into Bytes, KB, MB, GB and TB format.

Syntax

byte_format ( $num [, $precision = 1 ]);
  • $num: It defines the number of bytes passed in the byte_format () function.
  • $precision: It defines the number of floating points can be precise in byte_format ().

Create a simple program of Number helper

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

Online.php

<?php class Online extends CI_Controller
 {
     public function hello_function() {
     $this->load->helper('number');
 echo "Size in Bytes :".byte_format(576)."<br>"; 
 echo "Size in KB :".byte_format(7865)."<br>"; 
 echo "Size in KB :".byte_format(78658)."<br>";  
 echo "Size in MB :".byte_format(1472486)."<br>"; 
 echo "Size in GB :".byte_format(76589402356)."<br>"; 
 echo "Size in TB :".byte_format(1234567891234);
 }
 } 
 ?> 

When you execute the above program in localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Online, it shows the output, as shown below.

CodeIgniter Number helper

Path Helper

The path helper file has some predefined functions that allow you to access the file path on the server.

$this->load->helper (‘path’);

Function of the Path helper

1.Set_realpath (): This function is used to set and access the file or directory path on the server without create of symbolic links. And if the file path is not existing, it shows an error message on the server.

Syntax

set_realpath ( $path, $check_existance = FALSE ] );
  • $path : It takes the path of a particular file or folder as a string that can access on the server.
  • $check_existance : If you want to show an error message on the server when a particular file or directory is not valid, you can use a Boolean value such as true or false.

Example:

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

Online.php

<?php
 class Online extends CI_Controller
 { 
 public function set_path()  // It is the set_path method
 {   
 echo "<title> Tutorial and Example </title>";
     $this->load->helper('path'); // load path helper
     echo "SET REAL PATH :";
     $file_path = '/www/CodeIgniter-3.1.11/system/language/english';
 echo set_realpath($file_path); 
 echo "<br>";   
  $non_existent_file = '/application/to/non-defined-file_path.txt';
 echo set_realpath($non_existent_file, TRUE);  /* here TRUE is used for showing an error message on the server */
 }
 }
 ?> 

When you execute the above program in localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Online, it shows the output, as shown below.

CodeIgniter Number helper