- $fp = fopen ("/tmp/lock.txt", "w+");
- if (Flock ($FP, LOCK_EX)) {//row-type locking
- Fwrite ($fp, "Write something Here");
- Flock ($FP, lock_un); Release lock
- } else {
- echo "couldn ' t lock the file!";
- }
- Fclose ($FP);
- ?>
Copy CodeNote: 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). Note: Flock () cannot be used for NFS and some other network file systems. See the documentation for your operating system for more information. Flock () is implemented at the process level in some operating systems. When using a multithreaded server API (such as ISAPI), you may not be able to rely on flock () to protect files, because PHP scripts that run on other parallel threads in the same server instance can process the file. Flock () does not support legacy file systems, such as FAT and its derived systems. Therefore, this environment always returns FALSE (especially for Windows 98 users). Introduction to the Flock function usage of file lock function in PHP: Syntax: bool Flock (int $handle, int $operation [, int & $wouldblock]) The handle of the flock () operation must be a file pointer that has already been opened. Operation can be one of the following values: 1. To get a shared lock (read program), set operation to Lock_sh (PHP 4.0.1 Previous version set to 1) 2. To obtain an exclusive lock (write program), set operation to LOCK_EX (2 in the previous version of PHP 4.0.1) 3. To release a lock (regardless of share or exclusive), set operation to Lock_un (set to 3 in previous versions of PHP 4.0.1) 4. If you don't want flock () to block when locked, Operation plus LOCK_NB (set to 4 in previous versions of PHP 4.0.1) Look at the following code. Files: 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);
- ?>
Copy CodeFiles: b.php
- $file = "Temp.txt";
- $fp = fopen ($file, ' R ');
- Echo fread ($fp, 100);
- Fclose ($FP);
- ?>
Copy CodeAfter running a.php, run b.php immediately, you can see the output: ABC and other a.php run after running b.php, you can see the output: abc123 obviously, when the a.php write the file data is too large, resulting in a long time, then b.php read data is incomplete, in the b.php do Modify. Modify the b.php to:
- $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);
- ?>
Copy CodeAfter running a.php, run b.php immediately, you can find that b.php will wait until the completion of the a.php run (that is, 10 seconds) before the display: abc123 read the data intact, but the time is too long, he will wait for the write lock release, and then modify the b.php. Modify the b.php to:
- $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);
- ?>
Copy CodeAfter 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, not as long as you want to. The script of the small part of the conclusion: the proposed file cache, select the relevant lock, otherwise it may result in incomplete reading data, or write data repeatedly. File_get_contents seems to choose not to lock, do not know what his default lock, anyway, and the output is not locked, is incomplete data. |