PHP file lock write instance parsing,
This document describes how to write PHP files to multiple threads. The Code is as follows:
Function file_write ($ file_name, $ text, $ mode = 'A', $ timeout = 30) {$ handle = fopen ($ file_name, $ mode ); while ($ timeout> 0) {if (flock ($ handle, LOCK_EX) {// arrange its locking $ timeout --; sleep (1 );}} if ($ timeout> 0) {fwrite ($ handle, $ text. '\ n'); flock ($ handle, LOCK_UN); fclose ($ handle); // return true;} return false ;}
The handle operated by the flock (int $ handle, int $ operation) function must be an opened file pointer.
Operation can be one of the following values:
To obtain the shared lock (read Program), set operation to LOCK_SH (PHP 4.0.1 and earlier versions to 1 ).
To obtain an exclusive lock (Write Program), set operation to LOCK_EX (set to 2 in versions earlier than PHP 4.0.1 ).
To release the lock (whether shared or exclusive), set operation to LOCK_UN (3 in PHP versions earlier than 4.0.1 ).
If you do not want flock () to be blocked during the lock, add LOCK_NB to operation (set to 4 in versions earlier than PHP 4.0.1 ).
In addition,Fclose () is used to release the lock operation and is called when the code is executed.