PHPflock file lock. Description of flock function usage on w3school: PHPflock () function definition and usage flock () function lock or release files. If yes, true is returned. If it fails, false is returned. // Usage of flock functions on w3school:
PHP flock () function
Definition and usage
The flock () function locks or releases files.
If yes, true is returned. If it fails, false is returned.
Syntax
Flock (file, lock, block)
Parameter description
File is required. Specifies the opened files to be locked or released.
Lock is required. Specifies the type of lock to be used.
Optional. If it is set to 1 or true, other processes are blocked when the lock is performed.
Description
The flock () operation file must be an open file pointer.
The lock parameter can be one of the following values:
To obtain the shared lock (read program), set lock to LOCK_SH (PHP 4.0.1 and earlier versions to 1 ).
To obtain an exclusive lock (write program), set lock to LOCK_EX (set to 2 in versions earlier than PHP 4.0.1 ).
To release the lock (whether shared or exclusive), set lock to LOCK_UN (set it to 3 in versions earlier than PHP 4.0.1 ).
If you do not want the flock () to be blocked during the lock, add LOCK_NB to the lock (set it to 4 in PHP versions earlier than 4.0.1 ).
Tips and comments
Tip: you can use fclose () to release the lock operation, which is automatically called when the code is executed.
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 ").
Example
========================================================== ============
From: http://hxsdit.com/1110
PHP comes with the file lock function:
Bool flock (int $ handle, int $ operation [, int & $ wouldblock])
$ Handle is the pointer to the opened file;
$ Operation can be
"LOCK_SH", shared lock; "LOCK_EX", exclusive lock; "LOCK_UN", release lock; "LOCK_NB" to prevent regular congestion of the flock lock.
Here we will mainly talk about "LOCK_EX" and "LOCK_NB ".
For example, we have two files.
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, and then run flockb. php immediately.
Result:
11111111
22222222
22222222
22222222
22222222
22222222
11111111
11111111
11111111
11111111
This indicates that when no filelock is applied, the two files write the txt files at the same time.
Next, modify the code of the two php files.
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 );
Run flocka. php and then run flockb. php immediately.
We will find that flockb. php remains in the waiting state until the flocka. php operation is complete. flockb. php will continue to be executed only after the flocka. php operation is complete.
Output result:
11111111
11111111
11111111
11111111
11111111
22222222
22222222
22222222
22222222
22222222
In addition, when flock is executed, the file lock is automatically released.
Define and use the nested PHP flock () function to lock or release files. If yes, true is returned. If it fails, false is returned ....