Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PHP flock() Function

PHP flock() Function


PHP Filesystem Reference Manual Complete PHP Filesystem Reference Manual


Definition and Usage

The flock() function locks or releases a file.

Returns TRUE on success, or FALSE on failure.

Syntax

flock(file,lock,block)

Parameters Description
file Required. Specifies the open file to lock or release.
lock Required. Specifies which type of lock to use.

Possible values:

  • LOCK_SH - Shared lock (reader). Allows other processes to access the file.
  • LOCK_EX - Exclusive lock (writer). Prevents other processes from accessing the file.
  • LOCK_UN - Release a shared or exclusive lock
  • LOCK_NB - Avoid blocking other processes while locking.
  • | | block | Optional. If set to 1, other processes are blocked while the lock is being acquired. |

    Tips and Notes

    **Note:** These locks are only used within the current PHP process. If permissions allow, other processes may modify or delete a PHP-locked file.

    **Note:** flock() is mandatory under Windows.

    **Tip:** The lock can be released via fclose(), which is called automatically when the script finishes.


    Example


    PHP Filesystem Reference Manual Complete PHP Filesystem Reference Manual