php对txt文件的创建,写入,读取

示例 :

//读取txt文件
public function RobotIdFileRed()
{
    if(file_exists("文件路径")){
        $robotIdArr  = json_decode(file_get_contents("文件路径"),true);
        return $robotIdArr;
    } else {
        //tp错误提示
        $this->error('读取机器人失败');
    }

}

//创建txt文件并写入内容
public function RobotIdFileWrite($new_data)
{
    if(!file_exists("文件路径")){
        $myfile = fopen(""文件路径"", "w");
    }
    if(filesize("文件路径") === 0){
        file_put_contents("文件路径", json_encode($new_data));
    } else {
        $old_data  = json_decode(file_get_contents("文件路径"),true);
        $all_data = array_merge($old_data,$new_data);
        file_put_contents("文件路径", json_encode($new_data));
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_40154252/article/details/103278066