I\'m a little puzzled if I can spare the fclose command by just unsetting the variable that carries the handle?
$handle = fopen($file);
...
fclo
PHP docs hint that all resources with no remaining references are "freed", I assume for file handles this would include closing the file.
Simple test case:
$f = fopen("test.php", "r");
if (!flock($f, LOCK_EX)) {
print("still locked\n");
exit;
}
unset($f);
sleep(5);
print("goodbye\n");
(I've saved this as test.php, so it is locking itself; might need to change the filename in the fopen() to some existing file otherwise)
Run the script twice within 5 seconds; if you get "still locked", then apparently unsetting the handle did not release the lock. In my test, I did not get "still locked", so apparently unsetting the handle at least releases the lock, though it would seem silly to release locks upon garbage collection, but not close the file.