Emmanuel Istace
Emmanuel Istace

Reputation: 1249

Thread safe file writing in PHP

I need to log when and who uses one of my application.

For that, the actual simple solution would be to contact a server and write a log. I have a LAMP server hosting my project website. I'm not a php/web developer.

I'm afraid about concurrent writings, what if two of my clients download the log page at the same time ? Are the php fopen/fwrite/fclose methods thread safe ?

Upvotes: 5

Views: 1883

Answers (1)

Barmar
Barmar

Reputation: 781721

You can lock the log file around the logging function:

flock($fh, LOCK_EX);
fwrite($fh, $message);
flock($fh, LOCK_UN);

Upvotes: 4

Related Questions