PHP flock() Function

PHP flock Function


Definition : 

The PHP flock() function is used to lock or release a file. The lock is released also by fclose(), or when stream is garbage collected.

Syntax

  flock(filelockblock)

Parameter
Parameter Description
file Required. Specifies an open file to lock or release
lock Required. Specifies what kind of lock to use.

Possible values:

  • LOCK_SH - A shared lock (reader). Allow other processes to access the file
  • LOCK_EX - An exclusive lock (writer). Prevent other processes from accessing the file
  • LOCK_UN - Release the lock
  • LOCK_NB - Avoid blocking other processes while locking

Given below example Lock and release a "demo.txt" file:

Output : flock() function example