Is there a risk in running file_put_contents() on the same file from different PHP threads?

后端 未结 2 880
忘掉有多难
忘掉有多难 2020-12-09 08:23

I know file_put_contents() makes it really easy to append data to a file in PHP. I\'d like to try using PHP \"threads\" to file_put_contents() to the same log f

2条回答
  •  渐次进展
    2020-12-09 09:16

    Simple answer, yes. clashes can occur

    use something like file_put_contents($location, $data, FILE_APPEND | LOCK_EX);

    When you expect multiple instances to write to the same file, you should acquire an exclusive lock so no other processes can write to the file until the current one has finished writing it's data

提交回复
热议问题