Shut down and unlock a file:
<?php $file = fopen(“test.txt”,“w+”); // exclusive lock if (flock($file,LOCK_EX)) { fwrite($file,“Add some text to the file.”); fflush($file); // release lock flock($file,LOCK_UN); } else { echo “Error locking file!”; } fclose($file); ?> |
A file is locked and released by the flock() function.Reword
flock(file, lock, block) |
Parameter |
Description |
file |
Essential. To lock or release an open file, specify it. |
lock |
Essential. indicates the type of lock to be used. Potential amounts · LOCK_SH: A lock that is shared (reader). Permit file access to other processes · LOCK_EX: A writer lock that is exclusive. Stop other programs from opening the file. · LOCK_UN: Unlock the door · LOCK_NB: Don’t obstruct other processes when you’re locking |
block |
Not required. Set to 1 to lock and prevent other processes from opening. |
Return Value: |
FALSE when it fails, TRUE when it succeeds |
PHP Version: |
4.0+ |
PHP Changelog: |
PHP 5.5: Windows users now have support for the block parameter |