note 115558 added to function.flock
| From: | sinus-php at sinpi dot net | Date: | Fri, 15 Aug 2014 14:01:35 +0000 |
| Subject: | note 115558 added to function.flock | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-200194@lists.php.net to get a copy of this message | ||
"Assigning another value to handle argument in subsequent code will release the lock."
Note: this is also true for losing the handle's context completely (like returning from a
function, in which the handle was a local variable).
<?php
touch("testfile");
function mylock() {
$F1 = fopen("testfile","r");
if (flock($F1,LOCK_EX|LOCK_NB)) echo "First lock OK\n"; else echo "First lock
FAIL\n";
$F2 = fopen("testfile","r");
if (flock($F2,LOCK_EX|LOCK_NB)) echo "Second lock OK\n"; else echo "Second lock
FAIL\n";
}
mylock();
echo "Function returned.\n";
mylock();
unlink("testfile");
?>
Prints out:
First lock OK
Second lock FAIL
Function returned.
First lock OK
Second lock FAIL
This will lock the testfile, then attempt to lock it again with a new file handle (obviously
failing). Trying this again, though, results in proper locking again (and then failing again),
because when exiting the function both handles are lost and automatically unlocked.
----
Server IP: 72.52.91.14
Probable Submitter: 83.4.167.65
----
Manual Page -- http://php.net/manual/en/function.flock.php
Edit -- https://master.php.net/note/edit/115558
Del: integrated -- https://master.php.net/note/delete/115558/integrated
Del: useless -- https://master.php.net/note/delete/115558/useless
Del: bad code -- https://master.php.net/note/delete/115558/bad+code
Del: spam -- https://master.php.net/note/delete/115558/spam
Del: non-english -- https://master.php.net/note/delete/115558/non-english
Del: in docs -- https://master.php.net/note/delete/115558/in+docs
Del: other reasons-- https://master.php.net/note/delete/115558
Reject -- https://master.php.net/note/reject/115558
Search -- https://master.php.net/manage/user-notes.php