DonCoder Posted December 16, 2011 Share Posted December 16, 2011 Do race conditions usually exist when writing OR appending to an existing file? $f1 = fopen($filename1, 'r+'); $f2 = fopen($filename2, 'a'); flock($f1, LOCK_EX); flock($f2, LOCK_EX); fwrite($f1, 'foo'); fwrite($f2, 'foo'); flock($f1, LOCK_UN); flock($f2, LOCK_UN); fclose($f1); fclose($f2); What would happen if this code ran at the exact same time? Is there a chance that the data could be corrupted in the aforementioned code example? Quote Link to comment https://forums.phpfreaks.com/topic/253290-race-conditions-file-appendages/ Share on other sites More sharing options...
sunfighter Posted December 16, 2011 Share Posted December 16, 2011 Do the files separately and use an if statement: $f1 = fopen($filename1, 'r+'); if (flock($f1, LOCK_EX)) { do your stuf here; } else { echo "Couldn't get the lock!"; } fclose($f1); Quote Link to comment https://forums.phpfreaks.com/topic/253290-race-conditions-file-appendages/#findComment-1298582 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.