dpa31905 2015-06-28 05:48
浏览 35
已采纳

如何避免使用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);
}
  • 写回答

1条回答 默认 最新

  • duan0531 2015-06-28 07:20
    关注

    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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)