View Full Version : are their concurrency issues when reading/writing with a text file
brothercake
10-10-2002, 02:04 AM
I'm building a simple high-score table to go with a game, which stores the info in a text file.
Do I need to think about concurrency? if so, is there a method for dealing with it.
Spookster
10-10-2002, 07:37 AM
Yes it would be a good idea to lock the file while it is being written to or read from. Here is an example script where I am writing to a log file:
<?php
$file_name = "accesslog.txt";
$data = date("l, d-M-Y H:i:s T").",".$REMOTE_ADDR.",".$HTTP_USER_AGENT."\n";
$file_pointer = fopen($file_name, "a");
$lock = flock($file_pointer, LOCK_EX);
if ($lock){
fputs($file_pointer, $data);
flock($file_pointer, LOCK_UN);
}
fclose($file_pointer);
?>
This locks the file while being written to. Any requests to write to it while it is locked are put into a queue so they have to wait their turn.
brothercake
10-10-2002, 04:00 PM
That's very helpful, thanks :thumbsup:
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.