Atomically appending a line to a file and creating it if it doesn't exist

前端 未结 2 1812
無奈伤痛
無奈伤痛 2021-02-06 22:15

I\'m trying to create a function (for purposes of logging)

append($path, $data)

that

  1. creates $file if it doesn\'t exist and
2条回答
  •  无人及你
    2021-02-06 23:16

    PHP already has a built-in function to do this, file_put_contents(). The syntax is:

    file_put_contents($filename, $data, FILE_APPEND);
    

    Note that file_put_contents() will create the file if it does not already exist (as long as you have file system permissions to do so).

提交回复
热议问题