Should be done with file_put_contents and flag FILE_APPEND and LOCK_EX
http://php.net/manual/en/function.file-put-contents.php