note 39216 added to function.flock
| From: | BWO at rn2 dot php dot net | Date: | Wed, 21 Jan 2004 01:41:00 +0000 |
| Subject: | note 39216 added to function.flock | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-63913@lists.php.net to get a copy of this message | ||
Using a secondary file for locking isn't too bad. You simply surround your actual writes by
the flock commands on the locking file. Because of the blocking, your writes will not execute until
you get the exclusive lock.
<?php
$filename = "data.txt";
$lock = ".lock";
// Lock the .lock file.
$lock_fp = fopen($lock, "r");
if (!flock($lock_fp, LOCK_EX)) {
echo "locking failed";
}
// Write to the data file.
$fp = fopen($filename, "w");
fwrite( $fp, "word up!");
fclose($fp);
// Release the lock.
flock($lock_fp, LOCK_UN);
?>
Easy as pi.
----
Manual Page -- http://www.php.net/manual/en/function.flock.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+39216
Delete -- http://master.php.net/manage/user-notes.php?action=delete+39216&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+39216&report=yes
Search -- http://master.php.net/manage/user-notes.php