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 load the helper in the controller’s files.

Syntax

The function of File Helpers

  1. read_file(): It is used to read the contents of a files from the server that is stored in the root folder.

Syntax

Or

Before using the read function, you should create a folder named Files. In this folder create a file and write some text into it that can display on the local server.

CodeIgniter File Helper

In our case, we have created a file named myfile.txt file in the CodeIgniter-3.1.11/Files folder. In the files, we have written the following text.

Hello Friends, Welcome to the tutorial and example. You can develop a web application in CodeIgniter.

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/readFiles, it shows the content of the file, as shown below.

CodeIgniter File Helper

2. write_file(): It is used to update or modify an existing file that calls to the server by defining a path.

Syntax

  • $path: It defines the path of the file.
  • $data: It defines the new data that we want to update.
  • $mode: It defines the mode of the file whether the file is allowed to be read or write. By default, it is set to ‘wb’.

Example: Now 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/writeFiles It shows the message File has been updated! as shown below.

CodeIgniter File Helper

Now open myfile.txt from the CodeIgniter-3.1.11/Files/, it shows the updated text as shown below:

CodeIgniter File Helper

Note:  The write_file() function is used to write content to the specified path. If the file does not exist in the defined path, it creates a new file, automatically. So, you have to make small changes in the path like ‘./files/myfile1.txt’. Then it will show a new file in the defined path as ‘myfile1.txt’.

3. delete_files(): It is used to delete the existing files from the system.

Syntax

Parameters

  • $path: It defines the path of the file that we want to delete.
  • $del_dir: You can set it to true, if you want to delete the folder or the directory, else set false.
  • $htdocs: It uses Boolean value for skipping the deletion of .htaccess and index.php files or not.

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/deleteFiles, it shows the output, as shown below.

CodeIgniter File Helper

In the above image, 1 denotes that the file has been deleted successfully. Otherwise, it shows 0, it means your file hasnot been deleted.  If you want to delete the folder or directory with files, use the syntax, given below:

$datas = ‘./files/’;

$del = delete_files( $datas, True);

4. get_filenames(): As the name suggests, it fetches all the files names on the local server that you have defined in the root folder.

Syntax

  • $source_dir: It takes the path of the folder for shows all the filenames of the defined path in the local server.
  • $include_path: It uses the Boolean value to show the complete path of the files. The FALSE value represents the current path of the files, whereas TRUE value shows the complete path of the file.

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/getFiles, it shows the output, as shown below.

CodeIgniter File Helper

5. get_dir_file_info(): This function is used to get a complete description of all files in a folder or directory.

Syntax

  • $source_dir: It shows the directory path which you want to display on the server.
  • $top_level_only (boolean): It shows the data of a particular folder.

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/getdirFiles, it shows the output, as shown below.

CodeIgniter File Helper

The above image shows all the details about the folder or directory such as file names, server, size of files, date and the path of the files.

6. get_file_info(): It is used to show the details of the particular file such as file name, server, date, size and the path. It takes path of file as input from.

Syntax

  • $file: It shows the particular file path in the system.
  • $returned_values (array): It defines the type of information you want to retrieve from server such as name, server, size and date.

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/getInfo, it shows the details of the file tests.php, as shown below.

CodeIgniter File Helper

7. get_mime_by_extension(): It defines the extension of the file. It takes file name (with extension) as input and return the type of mime file.

Syntax

$filename: It defines the file name with extension.

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/getMime, it shows the output, as shown below.

Pin It on Pinterest

Share This