flock

flock -- portable file locking

Description

bool flock(int fp, int operation);

PHP supports a portable way of locking whole files in an advisory way (which means all accessing programs have to use the same way of locking or it will not work).

To lock a file, you have to open it first and pass the resulting file pointer to flock. The operations you can use are 1 for locking the file as a reader (shared lock), 2 for locking the file as a writer (exclusive lock) and 3 for releasing a lock. This enables a simple reader/writer model which can be used on almost every platform (including most Unixes and even Windows).

If you add 4 to the operation parameter, locking will be tried in a non-blocking way. It returns false, if the lock cannot be acquired non-blocking. Otherwise, flock will always return true.