PHPflock file lock details _ php Basics-php Manual

Source: Internet
Author: User
Tags php basics
The script house editor will explain how to use the file lock function flock function in php. I hope you can learn more about flock in Linux through this article.
(PHP 4, PHP 5)

Flock-lightweight consultation file locking

Description
Bool flock (int $ handle, int $ operation [, int & $ wouldblock])
PHP supports a lightweight method of locking all files in the consulting mode (that is, all access programs must be locked in the same way, otherwise it will not work.

Note:

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 ).

Returns TRUE if the call succeeds, or FALSE if the call fails.


Example #1 flock () Example

The code is as follows:


$ Fp = fopen ("/tmp/lock.txt", "w + ");
If (flock ($ fp, LOCK_EX) {// sort it to lock
Fwrite ($ fp, "Write something here ");
Flock ($ fp, LOCK_UN); // release lock
} Else {
Echo "Couldn't lock the file! ";
}
Fclose ($ fp );
?>


Note:

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

Warning
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 98 users ).

Usage of the flock function in php:

Syntax:

Bool flock (int $ handle, int $ operation [, int & $ wouldblock])
The handle of the flock () operation must be an opened file pointer. Operation can be one of the following values:
1. to obtain the shared Lock (read program), set operation to LOCK_SH (PHP 4.0.1 and earlier versions to 1)
2. to obtain an exclusive lock (write program), set operation to LOCK_EX (set to 2 in versions earlier than PHP 4.0.1)
3. to release the lock (whether shared or exclusive), set operation to LOCK_UN (set it to 3 in versions earlier than PHP 4.0.1)
4. if you do not want flock () to be blocked during locking, add LOCK_NB to operation (set it to 4 in PHP versions earlier than 4.0.1)

See the following code:

A. php

The code is as follows:


$ File = “temp.txt ";
$ Fp = fopen ($ file, 'w ');
If (flock ($ fp, LOCK_EX )){
Fwrite ($ fp, "abc ");
Sleep (10 );
Fwrite ($ fp, 123 ");
Flock ($ fp, LOCK_UN );
}
Fclose ($ fp );
?>


B. php

The code is as follows:


$ File = “temp.txt ";
$ Fp = fopen ($ file, 'r ');
Echo fread ($ fp, 100 );
Fclose ($ fp );
?>


After running a. php, run B. php immediately. the output is displayed:
Abc
After a. php is run, run B. php. the output is as follows:
Abc
123
Apparently, when a. php writes a file with a large amount of data, resulting in a long time, B. php reads incomplete data and modifies B. php.
Modify B. php:

The code is as follows:


$ File = “temp.txt ";
$ Fp = fopen ($ file, 'r ');
If (flock ($ fp, LOCK_EX )){
Echo fread ($ fp, 100 );
Flock ($ fp, LOCK_UN );
} Else {
Echo "Lock file failed ...";
}
Fclose ($ fp );
?>


After running a. php, run B. php immediately. you can find that B. php will not be displayed until a. php is completed (10 seconds later:
Abc
123
Reading data is complete, but it takes a long time. he has to wait for the write lock to be released and then modify B. php.
Modify B. php:

The code is as follows:


$ File = “temp.txt ";
$ Fp = fopen ($ file, 'r ');
If (flock ($ fp, LOCK_SH | LOCK_NB )){
Echo fread ($ fp, 100 );
Flock ($ fp, LOCK_UN );
} Else {
Echo "Lock file failed ...";
}
Fclose ($ fp );
?>


After running a. php, run B. php immediately. the output is displayed:
Lock file failed...
It proves that the lock file failure status can be returned, rather than waiting for a long time as it goes up.
Conclusion:
We recommend that you select related locks when caching files. Otherwise, the read data may be incomplete or the data may be written repeatedly.
File_get_contents does not seem to be able to select a lock and does not know what locks it uses by default. the reverse is the same as the output obtained by the unlock, which is incomplete data.

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.