W3school above describes the use of flock functions:
PHP flock () function
Definition and usage
The flock () function locks or releases files.
If successful, returns TRUE. If it fails, it returns false.
Grammar
Flock (File,lock,block)
Parameter description
File required. Specifies the open files to lock or release.
Lock required. Specifies which type of lock to use.
Block is optional. If set to 1 or true, the other process is blocked when the lock is made.
Description
The file for the flock () operation must be a pointer to an already opened document.
The lock parameter can be one of the following values:
To get a shared lock (read program), set lock to Lock_sh (previous version of PHP 4.0.1 to 1).
To obtain an exclusive lock (written program), set the lock to LOCK_EX (the previous version of PHP 4.0.1 to 2).
To release the lock (regardless of share or exclusive), set lock to Lock_un (the previous version of PHP 4.0.1 to 3).
If you do not want flock () to block when locked, add LOCK_NB to lock (the previous version of PHP 4.0.1 is set to 4).
Hints and Notes
Tip: You can release the lock operation through Fclose (), which is also called automatically when the code is executed.
Note: Because flock () requires a file pointer, you may have to use a special lock file to protect access to files that you intend to open in write mode (add "w" or "w+" in the fopen () function).
Example
==================================================
from:http://hxsdit.com/1110
PHP comes with a file lock function:
BOOL Flock (int $handle, int $operation [, int & $wouldblock])
$handle is the open file pointer;
$operation can be
"Lock_sh", shared lock, "LOCK_EX", exclusive lock, "Lock_un", Release lock, "LOCK_NB" to prevent blockage when flock is locked.
Here the main talk about "LOCK_EX" and "LOCK_NB".
For example, we have two files, as follows.
flocka.php
? View Code PHP
1 2 3 4 5 6 7 8 9 10
$file = ' temp.txt '; $fp = fopen ($file, ' a '); for ($i = 0; $i < 5; $i + +) {fwrite ($fp, "11111111\n"); sleep (1);} fclose ($FP);
flockb.php
? View Code PHP
1 2 3 4 5 6 7 8 9
$file = ' temp.txt '; $fp = fopen ($file, ' a '); for ($i = 0; $i < 5; $i + +) {fwrite ($fp, "22222222\n");} fclose ($FP);
Run flocka.php first, then run flockb.php immediately.
Results:
11111111
22222222
22222222
22222222
22222222
22222222
11111111
11111111
11111111
11111111
Note When no file lock is added, two files write to the TXT file at the same time.
Modify the code for the two PHP files below.
flocka.php
? View Code PHP
1 2 3 4 5 6 7 8 9 10 11 12 13
$file = ' temp.txt '; $fp = fopen ($file, ' a '); if (Flock ($FP, lock_ex)) {for ($i = 0; $i < 5; $i + +) {fwrite ($fp, "11111111\n"); sleep (1);} flock ($fp, Lock_un);} fclose ( $FP);
flockb.php
? View Code PHP
1 2 3 4 5 6 7 8 9 10 11 12 13
$file = ' temp.txt '; $fp = fopen ($file, ' a '); if (Flock ($FP, lock_ex)) {for ($i = 0; $i < 5; $i + +) {fwrite ($fp, "22222222\n");} flock ($fp, Lock_un);} fclose ($FP);
Also run flocka.php first, and then run flockb.php immediately.
You will find that flockb.php is waiting until the end of the flocka.php run, and flockb.php will not continue until the flocka.php is finished.
Output Result:
11111111
11111111
11111111
11111111
11111111
22222222
22222222
22222222
22222222
22222222
In addition, when the flock is executed, the file lock is automatically released.
http://www.bkjia.com/PHPjc/478698.html www.bkjia.com true http://www.bkjia.com/PHPjc/478698.html techarticle //w3school above describes the usage of the flock function: PHP flock () function definition and usage flock () function locks or frees files. If successful, returns TRUE. If it fails, it returns false. ...