PHP file lock function flock ()

Source: Internet
Author: User
Tags flock
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.