PHP Flock function Instance _php tutorial

Source: Internet
Author: User
Because Flock () requires a file pointer, you may have to use a special lock file to protect access to the files that you intend to open through write mode (add "w" or "w+" in the fopen () function).

Because Flock () requires a file pointer, you may have to use a special lock file to protect access to the files that you intend to open through write mode (add "w" or "w+" in the fopen () function).
fp = fopen ("test.txt", ' ab '); From the end
Flock ($FP, LOCK_EX); Lock the file for waiting ...
Fwrite ($fp, ' just a test string ... '); Start writing ...
Flock ($FP, lock_un); Release Write lock
Fclose ($FP); Close the file read operation:

Read
$fp = fopen ("test.txt", ' R ');
Flock ($FP, lock_sh);
Read from the file .....
Flock ($FP, lock_un);

!--? PHP Tutorial

$fp = fopen ("/tmp/lock.txt", "w+");

If (Flock ($FP, LOCK_EX)) {//For exclusive locking
fwrite ($fp, "write Something Heren"),
flock ($fp, lock_un);//Interpretation Put lock
} else {
echo "couldn& #39; t lock the file!";
}

Fclose ($fp);

? >
flock () cannot be used for NFS and some other network file systems. See the documentation for your operating system for more information. The

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 running on other parallel threads in the same server instance can handle the file


< span id= "url" itemprop= "url" >http://www.bkjia.com/phpjc/445408.html www.bkjia.com true http://www.bkjia.com/phpjc/445408.html techarticle because flock () requires a file pointer, Therefore, you may have to use a special lock file to protect access to files that you intend to open through write mode (in the fopen () function, add W or ...

  • & #xe63a;-->

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