Phpflock function _ PHP Tutorial

Source: Internet
Author: User
Phpflock function. The role of flock in php is to lock the file when operating the file. it is only available when the lock is canceled. The role of flock in php is to lock the file when operating the file. it is only available when the lock is canceled. F flock is used to lock files during file operations in php. it is only available when the lock is canceled.

The role of flock in php is to lock the file when operating the file. it is only available when the lock is canceled.

Flock
(PHP 4 and PHP 5)

Flock-portable consultation file locking

Description
Boolean group (resource $ processing, interpreting $ operations [Abstract & $ wouldblock])
Flock () allows you to perform a simple card reader/write mode, which can be used on almost all platforms (including most Unix derivatives or even Windows ).

The released lock is also fclose () (also known as automatic completion when the script is executed ).

PHP supports portable locking in a complete archive consulting method (that is, all access programs must be locked in the same way or it will not work ).

Parameters

Handle
An open file pointer.

Operation
Perform one of the following operations:

LOCK_SH to obtain the shared Lock (reader ).
LOCK_EX gets an exclusive lock (writer ).
LOCK_UN releases the lock (shared or dedicated ).
LOCK_NB if you do not want flock () to block and lock at the same time. (Windows is not supported)

Wouldblock
The third optional parameter is set to TRUE. if it is locked, it will be blocked (the EWOULDBLOCK errno condition ).


Return value
Returns TRUE or FALSE.

Modify

Version description
LOCK_XXX of The 4.0.1 constant is added. Before that, you must use 1 LOCK_SH, 2 LOCK_EX, 3 LOCK_UN, and 4 LOCK_NB


Instance

Example of #1 flock ()

$ Fp = fopen ("/tmp/lock.txt", "w + ");

If (flock ($ fp, LOCK_EX) {// do an exclusive lock
Fwrite ($ fp, "Write something heren ");
Flock ($ fp, LOCK_UN); // release the lock
} Else {
Echo "Couldn't lock the file! ";
}

Fclose ($ fp );

?>

Bytes. The role of flock in php is to lock the file when operating the file. it is only available when the lock is canceled. F...

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.