如何避免使用codeigniter覆盖文件写入

I have a code for file writing..the data is got it properly in controller. But the code is working in the for loop.. Iteration is ok. Disply all the data in controller Create a file log_date.txt but write only the last data is my problem..

$file_path='./application/assets/login/log_'.date'.txt',$data);
if(file_exists($file_path))
{
    write_file(APPPATH'/assets/login/log_'.$todate.'.txt', $data);
}
else
{
    write_file(APPATH'./assets/login/log_'.$todate.'.txt', $data);
}

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');