"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.