Codeigniter File Helper – Codeigniter file helper provides various functions which is used to access the file. $this->load->helper(‘file’); is used to load the helper. This contains the functions – read_file , write_file and delete_files. Here in this tutorial we are going to explain how to read , write and delete files in codeigniter.
Codeigniter File Helper Example
Now we will see how to load file helper in codeigniter and then use its function-
Load file Helper
How To Load File Helper in Codeingniter Example:
$this->load->helper('file'); |
Functions:-
Following functions are available in file helper.
- 1. Read File
- 2. Write File
- 3. Delete File
1. Read_File
Syntax of Read_File function is
Syntax of Read_File function is:-
read_file($fileName) |
Parameters:
- $fileName (string) : File path
- Returns : File contents or FALSE on failure
- Return type : string
2. write_file
Syntax of Write_File function is
Syntax of Write_File function is:-
write_file($path, $data[, $mode = 'wb']) |
Parameters:
- $path (string) : File Path
- $data (string) : Data to write to file
- $mode (string) : fopen() mode
2. Delete_files
Syntax of Delete_Files function is
Syntax of Delete_Files function is:-
delete_files($path[, $del_dir = FALSE[, $htdocs = FALSE]]) |
Parameters:
- $path (string) : File Path
- $del_dir (bool) (string) : Whether to also delete directories
- $htdocs (bool) : Whether to skip deleting .htaccess and index page files
- Returns : TRUE OR FALSE
- Return Type : Bool
EXAMPLE 1
Here is simple example of Read File.
| Read File in Codeigniter Example:-
public function readfiles() { $this->load->helper('file'); $path = 'http://localhost/CodeIgniter/media/write.txt'; $string = read_file($path); echo $string; } |
EXAMPLE 2
Here is simple example of Write File.
| Write File in Codeigniter Example:-
public function writefiles() { $this->load->helper('file'); $path = 'http://localhost/CodeIgniter/media/new.txt'; $data = 'Good Morning Friends'; if ( ! write_file($path, $data)) { echo 'Unable to write'; } else { echo 'File written!'; } } |
EXAMPLE 3
Here is simple example of Delete Files.
| Delete Files in Codeigniter Example:-
public function Deletefiles() { $this->load->helper('file'); $path = 'http://localhost/CodeIgniter/media/file/abc.txt'; delete_files($path, TRUE); } |