Bool flock (resource $ handle, int $ operation [, int & $ wouldblock])
Handle
A file system pointer is a resource typically created by fopen ).
Operation can be one of the following values:
1. LOCK_SH gets the shared lock (read program ).
2. LOCK_EX gets an exclusive lock (written Program.
3. LOCK_UN release lock (whether shared or exclusive ).
4. If you do not want flock () to be blocked during the lock, it is LOCK_NB (not supported on Windows ).
Wouldblock
If the lock is blocked (in case of EWOULDBLOCK error code), the optional third parameter is set to TRUE. (Not supported on Windows)
Return Value
Returns TRUE if the call succeeds, or FALSE if the call fails.
Before PHP 5.3.2, the lock will also be released by fclose () (automatically called after the script is completed). To unlock the lock, you must manually perform it (flock ($ fp, LOCK_UN ); // release the lock ).
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. (?)
Eg: 1
File_get_contents and file sometimes return null when reading a non-empty file, as shown in the following example:
Code2 outputs null during code1 lock. code3 will wait for code1 to release the lock and return the obtained content.
Code1
<?php$fo = fopen('abc.txt', 'r+');flock($fo, LOCK_EX);sleep(10);flock($fo, LOCK_UN);
Code2
<?phpvar_dump(file_get_contents('abc.txt'));var_dump(file('abc.txt'));
Code3
<?php$con = getContents('abc.txt');print_r($con);function getContents($path, $waitIfLocked = true) { if(!file_exists($path)) { throw new Exception('File "'.$path.'" does not exists'); } else { $fo = fopen($path, 'r'); $locked = flock($fo, LOCK_SH, $waitIfLocked); if(!$locked) { return false; } else { $cts = file_get_contents($path); flock($fo, LOCK_UN); fclose($fo); return $cts; } }}
Eg: 2
The running result of the local machine is inconsistent with the following. It can be written in LOCK_SH (???)
The following is an example in the manual.
I just spend a long time to understand why write function returns me "0", on a basic file opening and then writing.
I discovered that if you use LOCK_SH and then you write something, that will not work:
<?php$fp = fopen('file.txt', 'a');flock($fp,LOCK_SH);$written = fputs($fp, 'data');var_dump($written); // 0 and file is not changedfclose($fp);
Eg: 3
Write code to solve the problem of multi-process threads reading and writing a file at the same time:
PHP does not have the concept of multithreading. However, we can still simulate multithreading in an "imperfect" way.
In short, it is queue processing. You can lock and unlock files. When a file is operated by a user,
The file is locked, and other users can only wait. It is not perfect, but it can also meet the requirements of some applications.
<? Phpfunction T_put ($ filename, $ string) {$ fp = fopen ($ filename, 'A'); // enable if (flock ($ fp, LOCK_EX) in append Mode )) {// Add the write lock fputs ($ fp, $ string); // write the file flock ($ fp, LOCK_UN); // unlock} fclose ($ fp );} function T_get ($ filename, $ length) {$ fp = fopen ($ filename, 'R'); // enable if (flock ($ fp, LOCK_SH) in append Mode )) {// read lock $ result = fgets ($ fp, $ length); // read the file flock ($ fp, LOCK_UN); // unlock} fclose ($ fp ); return $ result ;}