note 65136 added to function.flock
| From: | MarkMiller at osu1 dot php dot net | Date: | Tue, 25 Apr 2006 02:41:55 +0000 |
| Subject: | note 65136 added to function.flock | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-109628@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 64.71.164.2
Probable Submitter: 216.66.115.97
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.flock.php
Edit -- http://master.php.net/note/edit/65136
Del: integrated -- http://master.php.net/note/delete/65136/integrated
Del: useless -- http://master.php.net/note/delete/65136/useless
Del: bad code -- http://master.php.net/note/delete/65136/bad+code
Del: spam -- http://master.php.net/note/delete/65136/spam
Del: non-english -- http://master.php.net/note/delete/65136/non-english
Del: in docs -- http://master.php.net/note/delete/65136/in+docs
Del: other reasons-- http://master.php.net/note/delete/65136
Reject -- http://master.php.net/note/reject/65136
Search -- http://master.php.net/manage/user-notes.php