×

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

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 Tutorial

What is CodeIgniter? CodeIgniter is an open-source framework for developing a dynamic web application with the use of PHP. It uses the MVC framework to develop the project, which is much...

3 minutes read.

CodeIgniter Routes

The Routes are used to handle URL requests on the browser. It matches the URL request to the predefined routes in CodeIgniter. Therefore, a file must be predefined as a routes.php file to...

6 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 Security Class

Security Class The security class provides various function that helps to create a secure application and process input data securely to the application. This class is preloaded in the CodeIgniter application, so you...

4 minutes read.

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

3 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.

CodeIgniter Security Helper

A Security helper file contains some predefined functions that are used to protect application from unauthorized access. Loading the Helper The following syntax is used to load the security helper in the CodeIgniter application. Syntax $this-> load->...

4 minutes read.

Update a Record CodeIgniter

Update a record In this topic, we will learn how we can update existing records in database tables. To update a record, CodeIgniter provides an update() function used with the set()...

10 minutes read.

Versions of CodeIgniter

CodeIgniter v3.1.11 (This is the current version which we will used in this tutorial)CodeIgniter v3.1.10CodeIgniter v3.1.9CodeIgniter v3.1.8CodeIgniter v3.1.7CodeIgniter v3.1.6CodeIgniter v3.1.5CodeIgniter v3.1.4CodeIgniter v3.1.3CodeIgniter v3.1.2CodeIgniter v3.1.1CodeIgniter v3.1.0CodeIgniter v3.0.6CodeIgniter...

1 minute read.

CodeIgniter Encrypt Class

The Codeigniter provides an encrypt class used to protect confidential data stored on computer systems or data transmitted over a network. It provides two-way data encryption techniques such as Mcrypt PHP extension and...

4 minutes read.

CodeIgniter Benchmarking Class

The CodeIgniter contains the Benchmarking class that is used to calculate the time difference between two points or the memory usage of the passed statements in the application. By default, the Benchmarking...

7 minutes read.

CodeIgniter Views

A View is a presentational part which shows the information to the users. It is a complex web page which contains navbar, header, footer, sidebar, etc. A view file cannot be called directly...

5 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.

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.

How to remove an index.php file from the Codeigniter URLs?

As we know, a URL in CodeIgniter is designed in such a way that it would be search engine and human friendly, too, rather than using a standard approach to create a...

2 minutes read.

Retrieve data from Database

Retrieve data from the Database Now we will discuss how we can retrieve data from the database that we have inserted to the employees table of the database in our previous topic “inserted...

2 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.

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.