×

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

Related Topics

Codeigniter Session Library

Session Library The session is an essential part of any application. It contains various functions to manage the users' status and track their activity while browsing on the site. For example,...

13 minutes read.

CodeIgniter CAPTCHA Helper

A CAPTCHA (Complete Automated Public Turning test to tell Computer and Humans Apart) helper is a function that is used in web application to check whether the user is human or machine. It...

3 minutes read.

Structure of Codeigniter

The File structure of CodeIgniter is divided into three parts: ApplicationSystemuser_guide Application -: As the name represents Application, which contains all the main parts of the project like a controller, libraries, view, models, and others....

3 minutes read.

CodeIgniter URL Helper

The helper file contains some predefined functions that are used to work with URL, such as generate the current file path, transfer control, redirect, create a link, etc. Load the URL Helper Before using...

7 minutes read.

Delete a record from Database CodeIgniter

Delete a record from the Database After inserting, retrieving, and updating table records, we will now learn how we can delete a particular record from a database table. To delete a record, first, we...

6 minutes read.

CodeIgniter Calendaring Library

The calendar library contains various functions that are used to create a dynamic calendar. It also enables you to pass the data to calendar cells and 100% control over the calendar design. Load...

7 minutes read.

Form Validation Library Codeigniter

Form Validation Library The Codeigniter provides a form validation library that contains predefined rules to validate the form fields. It also helps in optimizing the form validation code, which reduces the effort and time...

31 minutes read.

CodeIgniter Email Class

CodeIgniter provides an e-mail library that helps to send a message from one application to another. You can create and send text messages easily in CodeIgniter application even you can set email...

9 minutes read.

CodeIgniter Input Class

Input Class The CodeIgniter framework provides an input class that includes various input functions to pre-process global input data for protection; furthermore, it also provides some helper functions to obtain input data. This...

11 minutes read.

Installation of CodeIgniter

Before installing CodeIgniter, you need to make sure that the wamp or xampp server in the system is preinstalled. The installation procedure for CodeIgniter is very easy. You can download CodeIgniter by following these...

2 minutes read.

CodeIgniter File Helper

The file helper function is used to perform various tasks with files such as accessing files, writing data to files, deleting files, and more. Loading the File Helper Before using the file helper, you must...

7 minutes read.

CodeIgniter Array Helper

The Array Helper contains some predefined functions that are used to perform the various operation with array. Load Array helper It is used to load the helper class. We can pass the helper function in the...

6 minutes read.

CodeIgniter Language Class

Language Class The CodeIgniter’s language class contains the various functionality that is used to access the language-specific files and text line for the purposes of internationalization. This class provides you suitability to add or...

4 minutes read.

CodeIgniter Shopping Cart Class

It contains the various function that enables the user to add or update, display, and delete data item from the shopping carts while browsing the site. The items added in the cart...

10 minutes read.

Migration Class CodeIgniter

The CodeIgniter framework provides the migration class that helps the web developers to create and handle databases in a structured and streamlined manner. The migration classes are very useful when more than...

6 minutes read.

CodeIgniter Pagination Class

Pagination Class The CodeIgniter framework provides a pagination class that is used in the data list to create pagination links in a web application. Generally, it is used to retrieve data records from...

10 minutes read.

HTML Table Class CodeIgniter

HTML Table Class The CodeIgniter framework provides an HTML Table Class that is used to develop an auto-generated table using a defined array or result sets of the database table. Loading an HTML Table Class...

8 minutes read.

CodeIgniter HTML Helper

The HTML helper function is used in CodeIgniter to provide various functionality, such as headings, images, links, list tags, etc. Loading the HTML Helper Before using the html helper function, you must load the...

7 minutes read.

CodeIgniter Creating Library

As we know, CodeIgniter has a large collection of library functions that are stored in the system/libraries folder. CodeIgniter also provides some additional functionality in library files. For example, if you want to...

3 minutes read.

CodeIgniter String Helper

The string helper file contains functions that allows separate operations with strings in CodeIgniter. Loading the String Helper Before using the string helper in the CodeIgniter application, you must load it in the controller...

7 minutes read.