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

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

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

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

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

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

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

Pin It on Pinterest

Share This