Practical flock() implementation

From: Date: Sat, 23 Jun 2001 20:28:29 +0000
Subject: Practical flock() implementation
Groups: php.general 
Request: Send a blank email to php-general+get-54990@lists.php.net to get a copy of this message
I'm having trouble understanding the docs and annotations on flock(). I've got a script that needs to overwrite a data file's contents without letting Instance B of that script truncate the data file while Instance A is still writing new content to it. How do other people do this? It seems to me that if I fopen() in "w" or "w+" mode, then the file will be truncated before it can be determined whether there's a lock already in effect or the current instance can acquire a lock; but flock() requires a filepointer argument, so don't I *have* to fopen the file before going to flock()? Or should I fopen first in "r" mode, acquire the lock, then re-open in "w" mode, counting on the lock to stay in effect even though it's a different file pointer? I'd also be grateful if someone could clarify the difference between blocking and locking (i.e. "If you don't want flock() to block while locking, add LOCK_NB (4 prior to PHP 4.0.1) to operation."). TIA -- CC

« previous php.general (#54990) next »