Directory Helper Codeigniter

Directory Helper CodeIgniter: provides a directory helper function that works with the directory. It shows the structure of the file, hidden file in the folder, and the folder in root directory.

Loading the Directory Helper

Before using the directory helper function, you have to load the helper class in the controller’s files.

Syntax

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

Functions of Directory Helpers

  1. directory_map(): A directory_map() function is used for defining the source path, depth of the directories and the hidden files.

Syntax

directory_map( $source_dir [, $directory_depth = ‘ ‘ [, $hidden = ‘FALSE‘] ]);

Parameters Description

  • $source_dir (string): It defines the path of the directory you want to show
  • $directory_depth (int): It indicates the depth or level of the directory. It means that you can define the level of the directory which you want to show. Where level = 0 (denotes fully recursive), 1 shows the current level, 2 shows up to 2nd level, etc.
  • $hidden = It considers whether to show the hidden files or not.

Create multiple directory inside the root directory

Example: Create a Tests.php file in application/controller folder and write the following program.

Tests.php

<?php
 class Tests extends CI_Controller
  {
     public function getDirectory() // define getDirectory() function that call on the url
     {  
        $this->load->helper('directory');  // loading of directory helper
         echo "<title> Tutorial And Example </title>";  
       $data = directory_map('./system/'); // this function show all the directory/files inside the //System directory of CodeIgniter.
 echo "<pre>";
 print_r($data);
 }
 }
 ?> 

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

Directory Helper Codeigniter

The above output shows the multiple directory inside the root directory (system directory) in the CodeIgniter application.

Create a single Directory

If you want to display a single level inside the system directory, you need to define the level as numeric in directory_map(‘./system/’, 1). It shows a single directory.

Example: Create a Tests.php file in application/controller folder and write the following program.

Tests.php

<?php
 class Tests extends CI_Controller
  {
     public function getDirectory() // define getDirectory() function that call on the url
     {  
        $this->load->helper('directory');  // loading of directory helper
         echo "<title> Tutorial And Example </title>"; 
       $data = directory_map('./system/', 1);  // it will show a single level
 echo "<pre>";
 print_r($data);
 }
 }
 ?> 

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

Directory Helper Codeigniter

Display the Hidden files

If you want to display hidden files of the directory, set the Boolean value either true or false in the directory_map() function.

Syntax

directory_map(‘./directory_name/’,FALSE, TRUE); 

The first parameter represents the directory name. The second parameter represents to show directory name or not. If we want to show the directory name set the Boolean value to true else set false. The last parameter represents to show hidden files or not. If we want to show hidden files set the Boolean value true else set false.  

Example: Create a Tests.php file in application/controller folder and write the following program.

Tests.php

<?php
 class Tests extends CI_Controller
  {
     public function getDirectory() 
     {  
        $this->load->helper('directory');  
         echo "<title> Tutorial And Example </title>"; 
 $data1 = directory_map('./system/',TRUE,TRUE);  /* it shows hidden files with directory name */
 $data = directory_map('./system/', FALSE, TRUE);  /* it shows all the files including hidden file, False defines that it will show all the files name without the directory name, True represents that it will show only hidden files. */
 echo "<pre>";
 print_r($data1);
 print_r($data);
 }
 }
 ?> 

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

Directory Helper Codeigniter