Add new data into file codeigniter
In CodeIgniter, you can add new data into a file by using the write_file()
function from the file
library. Here's an example:
Step 1: Load the file library
In your controller, load the file library:
$this->load->library('file');
Step 2: Set the file path and name
Set the file path and name where you want to write the data:
$file_path = 'path/to/your/file.txt';
Step 3: Write the data to the file
Use the write_file()
function to write the data to the file:
$data = 'Hello, World!';
$this->file->write_file($file_path, $data);
Step 4: Save the changes
Save the changes to the file:
$this->file->save($file_path);
Here's the complete code:
<?php
class MyController extends CI_Controller {
public function add_data_to_file()
{
$this->load->library('file');
$file_path = 'path/to/your/file.txt';
$data = 'Hello, World!';
$this->file->write_file($file_path, $data);
$this->file->save($file_path);
echo 'Data written to file successfully!';
}
}
Note: Make sure to replace path/to/your/file.txt
with the actual path and name of the file you want to write to.
Also, if you want to append data to an existing file instead of overwriting it, you can use the append_to_file()
function instead of write_file()
.