I guess that if the file exists, then you want to append the data to it. Otherwise you want to create a file and add the data right? If this is your concern, then you can change the file writing mode with the optional 3rd parameter within the write_file
function. By default, it is
wb
where it overwrites the file. However with the a
method, you can append a new line to the file without overwriting it.
$file_path = APPPATH . "/assets/login/log_.$todate.txt";
if(file_exists($file_path))
{
write_file($file_path, $data, 'a');
}
else
{
write_file($file_path, $data);
}
Even better, you can just do what you want without the control statement as the a
mode checks first if the file exists or not, then creates it, otherwise it just appends the data. So just the below part is enough without the control statement.
$file_path = APPPATH . "/assets/login/log_.$todate.txt";
write_file($file_path, $data, 'a');
Edit:
To add each data within a new row when you are running it within a loop, add new line to $data
:
$file_path = APPPATH . "/assets/login/log_.$todate.txt";
write_file($file_path, $data . "
", 'a');