- $ 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, you may have to use a special lock file to protect access to the files to be opened in write mode (in fopen () add "w" or "w +") to the function "). Note: flock () cannot be used for 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]) 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 to 3 in versions earlier than PHP 4.0.1) 4. if you do not want flock () to be blocked during the lock, add LOCK_NB to operation (set it to 4 in PHP versions earlier than 4.0.1) Let's look at the following code. File: a. 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 );
- ?>
-
File: B. php
- $ File = “temp.txt ";
- $ Fp = fopen ($ file, 'r ');
- Echo fread ($ fp, 100 );
- Fclose ($ fp );
- ?>
-
Run. run B immediately after php. php, you can see the output: abc and so on. run php after running B. php, you can see the output: abc123 obviously, when. when php writes a file, the data volume is too large, resulting in a long time. in this case, B. php reads incomplete data. php. Modify B. 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 );
- ?>
-
Run. run B immediately after php. php, you can find B. php will wait until. after php is run (10 seconds later), it shows that abc123 reads data completely, but it takes a long time to wait for the write lock to be released, and then B. php. Modify B. 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. the output is 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: it is recommended that you select the relevant lock when caching the file. Otherwise, the read data may be incomplete or the data will be repeatedly written. 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. |