PHP in flock file lock detailed

Source: Internet
Author: User
Tags flock fread php write
    1. $fp = fopen ("/tmp/lock.txt", "w+");
    2. if (Flock ($FP, LOCK_EX)) {//row-type locking
    3. Fwrite ($fp, "Write something Here");
    4. Flock ($FP, lock_un); Release lock
    5. } else {
    6. echo "couldn ' t lock the file!";
    7. }
    8. Fclose ($FP);
    9. ?>
Copy Code

Note: 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

    1. $file = "Temp.txt";
    2. $fp = fopen ($file, ' w ');
    3. if (Flock ($FP, lock_ex)) {
    4. Fwrite ($FP, "abc");
    5. Sleep (10);
    6. Fwrite ($FP, "123");
    7. Flock ($FP, lock_un);
    8. }
    9. Fclose ($FP);
    10. ?>
Copy Code

Files: b.php

    1. $file = "Temp.txt";
    2. $fp = fopen ($file, ' R ');
    3. Echo fread ($fp, 100);
    4. Fclose ($FP);
    5. ?>
Copy Code

After 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:

    1. $file = "Temp.txt";
    2. $fp = fopen ($file, ' R ');
    3. if (Flock ($FP, lock_ex)) {
    4. Echo fread ($fp, 100);
    5. Flock ($FP, lock_un);
    6. } else{
    7. echo "Lock file failed ...";
    8. }
    9. Fclose ($FP);
    10. ?>
Copy Code

After 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:

    1. $file = "Temp.txt";
    2. $fp = fopen ($file, ' R ');
    3. if (Flock ($fp, Lock_sh | LOCK_NB)) {
    4. Echo fread ($fp, 100);
    5. Flock ($FP, lock_un);
    6. } else{
    7. echo "Lock file failed ...";
    8. }
    9. Fclose ($FP);
    10. ?>
Copy Code

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, 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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.