This article mainly introduces the PHP file lock function flock () in detail. This article focuses on the functions and parameters of the flock function, as well as issues needing attention when using it, if you need it, you can refer to the file operating system in the network environment. multiple client users may access the same file on the server at the same time. When such concurrent access is generated, it is likely to damage the file. For example, a user is writing data to a file. when the file is not written, other users write data to the file at this time, which will cause data writing confusion. In addition, when the user does not finish writing the data, other users will obtain the content in the file and will also obtain incomplete data.
The flock () function is provided in PHP to lock or release files ). When a process adds a lock to access a file, other processes must wait until the lock is released to access the file. This prevents data corruption when concurrently accessing the same file. The function is prototype as follows:
The code is as follows:
Bool flock (int handle, int operation [, int & wouldblock]) // locking the lightweight consulting file
The first parameter handle must be an opened file resource, and the second parameter opeation is also required, specifying which type to use. Operation can be one of the following values:
★LOCK_SH gets the Share Lock (used when reading data from a file ).
★LOCK_EX gets an exclusive lock (used when writing data to a file ).
★LOCK_UN releases a lock (whether shared or exclusive ).
★LOCK_NB additional Lock (if you do not want flock () to be blocked at the lock time, you should add the lock after the above lock ).
If the lock is blocked (if the object has been locked by flock (), The flock () function will be suspended when it is locked again, and the lock will become blocked ), you can also set the optional third parameter to 1. when the lock is performed, other processes will be blocked. The lock operation can also be released by fclose. In order for the flock () function to take effect, all programs that access files must use the same method to lock the file. If the function is successful, TRUE is returned. if the function fails, FALSE is returned.