PHP file lock

Source: Internet
Author: User
Tags flock
// Writeappsfpw.fopen(test.txt, 'AB'); // Fromtheendflock ($ fp, LOCK_EX); // lockthefileforwaiting... fwrite ($ fp, 'justateststring ....... '); // Startwritin

// WRITE
$ Fp = fopen ("test.txt", 'AB'); // From the end
Flock ($ fp, LOCK_EX); // lock the file for waiting...
Fwrite ($ fp, 'just A Test String ...... '); // Start writing...
Flock ($ fp, LOCK_UN); // Release write lock
Fclose ($ fp); // Close the file read operation:

// READ
$ Fp = fopen ("test.txt", 'r ');
Flock ($ fp, LOCK_SH );
// Read from the file .......
Flock ($ fp, LOCK_UN );
Fclose ($ fp); here is a detailed description of the function flock in the PHP manual:

Flock-lightweight consultation file locking

The function prototype is bool flock (int handle, int operation [, int & wouldblock]).

PHP supports a lightweight method of locking all files in consultation (that is, all access programs must be locked in the same way, otherwise it will not work). Note the following: in Windows, flock () will be executed forcibly. The handle of the flock () operation 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 ). Flock () allows you to execute a simple read/write model (including most Unix-derived versions and even Windows) that can be used on any platform ). If the lock is blocked (in case of EWOULDBLOCK error code), the optional third parameter is set to TRUE. The lock operation can also be released by fclose () (automatically called when the code is executed ). If the call succeeds, TRUE is returned. if the call fails, FALSE is returned.

Let's take a look at the classic instance above PHP manual:

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

If (flock ($ fp, LOCK_EX) {// sort it to lock
Fwrite ($ fp, "Write something here \ n ");
Flock ($ fp, LOCK_UN); // release lock
} Else {
Echo "Couldn't lock the file! ";
}

Fclose ($ fp );

?> Note: Because flock () requires a file pointer, you may have to use a special lock file to protect the access to the file to be opened in write mode (in fopen () add "w" or "w +") to the function ").

Note:

Flock () cannot be used in NFS or other network file systems. For more information, see your own operating system documentation.

In some operating systems, flock () is implemented at the process level. When a multi-threaded server API (such as ISAPI) is used, flock () may not be used to protect files, because PHP scripts running on other parallel threads in the same server instance can process the file.

Flock () does not support old file systems, such as FAT and its derived systems. Therefore, FALSE is always returned in this environment (especially for Windows

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.