note 65136 deleted from function.flock by nlopess
| From: | nlopess@php.net | Date: | Mon, 18 Dec 2006 11:14:34 +0000 |
| Subject: | note 65136 deleted from function.flock by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-120728@lists.php.net to get a copy of this message | ||
Note Submitter: Mark Miller
----
The key is to create an empty lock file that goes with the real file you want to protect.
$fp = fopen($lockFile, 'x');
fclose($fp);
Now open the lockfile in read mode
$this->lockFile = fopen($lockFile, 'r');
now grab your lock on it.
flock($this->lockFile, LOCK_EX + LOCK_NB);
Now you can access the real file using 'w' or 'w+' concurrently without
corruption. Always acquire a lock on the lock file before writing to it. I have not been able to
cause a problem by leaving out grabbing a reader lock so I will leave that up to you.