PHP flock file locks are described in detail

  • 2020-05-27 04:33:59
  • OfStack

flock
(PHP 4, PHP 5)

flock - lightweight consultation file locking

instructions
bool flock ( int $handle , int $operation [, int & $wouldblock ] )
PHP supports a portable way to lock all files in a consultative manner (that is, all access programs must be locked in the same way, otherwise it will not work).

Note:

flock() will be enforced under Windows.

The handle for the flock() operation must be a pointer to an open file. operation can be 1 of the following values:


To get the Shared lock (the program that read it), set operation to LOCK_SH (PHP was set to 1 before 4.0.1).
To get an exclusive lock (the program written), set operation to LOCK_EX (PHP was set to 2 in previous versions of PHP 4.0.1).
To release the lock (whether Shared or exclusive), set operation to LOCK_UN (PHP was set to 3 in previous versions of PHP 4.0.1).
If you don't want flock() to be blocked during locking, add LOCK_NB (set to 4 in previous versions of PHP 4.0.1) to operation.

flock() allows you to execute a simple read/write model that can be used on any platform (including most of the Unix derivatives and even Windows). If the lock is blocked (in the case of an EWOULDBLOCK error code), the optional third parameter is set to TRUE. The lock operation can also be freed by fclose(which is automatically called when the code is finished).

Returns TRUE on success or FALSE on failure.


Example #1 flock() example
 
<?php 
$fp = fopen("/tmp/lock.txt", "w+"); 
if (flock($fp, LOCK_EX)) { //  Exclusive locking is performed  
fwrite($fp, "Write something here "); 
flock($fp, LOCK_UN); //  Release the lock  
} else { 
echo "Couldn't lock the file !"; 
} 
fclose($fp); 
?> 

Note:

Since 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+" to the fopen() function).

Warning
flock() cannot be used on NFS and other network file systems. See the documentation for your operating system for details.
flock() is implemented at the process level in some operating systems. When using one multithreaded server, API (such as ISAPI), you may not be able to rely on flock() to protect the file, because PHP scripts running on other parallel threads in the same server instance can process the file.
flock() does not support older file systems, such as FAT and its derived systems. Therefore, FALSE is always returned in this environment (especially for Windows 98 users).

File lock function flock in php

Grammar:

bool flock (int $handle, int $operation [, int & $wouldblock])
The handle for the flock () operation must be a pointer to an open file. operation can be 1 of the following values:
1. To get the Shared lock (reader), set operation to LOCK_SH (PHP was set to 1 before 4.0.1)
2. To obtain an exclusive lock (writer), set operation to LOCK_EX (PHP was set to 2 in previous versions of PHP 4.0.1)
3. To release the lock (whether Shared or exclusive), set operation to LOCK_UN (PHP was set to 3 in previous versions of PHP 4.0.1)
4. If you don't want flock () to be blocked while locked, add operation to LOCK_NB (PHP was set to 4 before PHP 4.0.1)

See the following code:

a.php
 
<?php 
$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
 
<?php 
$file =  " temp.txt " ; 
$fp = fopen ( $file , 'r' );  
echo fread ( $fp , 100 );  
fclose ( $fp );  
?> 

After running a.php, run b.php immediately. You can see the output:
abc
When a. php is finished, run b. php, and you can see the output:
abc
123
Obviously, when a.php was writing a file with too much data, resulting in a relatively long time, then b.php was reading incomplete data and making modifications to b.php
Modify b.php is:
 
<?php 
$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 wait until a.php is finished (i.e., 10 seconds later) before displaying:
abc
123
The read was complete, but it took too long. He had to wait for the write lock to be released before making changes to b.php.
Modify b.php is:
 
<?php 
$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. You can see the output:
Lock file failed...
Prove that you can return the lock file failure status instead of having to wait as long as you did above.
This site this site conclusion:
It is recommended to select the appropriate lock when caching the file, otherwise it may result in incomplete reading or repeated writing of the data.
It seems that file_get_contents can't choose the lock, so I don't know what lock he USES by default. Anyway, it is not complete data with the output of 1 without the lock.

Related articles: