PHP uses flock to implement file locking

Source: Internet
Author: User
Tags flock
This article mainly introduces how PHP uses flock to implement file lock. The example analyzes the usage skills of flock file lock, for more information about how to use flock to implement file lock in PHP, see the following example. Share it with you for your reference. The specific analysis is as follows:

Flock () allows you to execute a simple read/write model (including most Unix-derived versions and even Windows) that can be used on any platform ). If the lock is blocked (in case of EWOULDBLOCK error code), set the third optional parameter to TRUE. The lock operation can also be released by fclose () (automatically called when the code is executed ).

To put it simply, it is to lock a file so that multiple processes are limited to access the file, thus preventing conflicts. For example:

<?php   $file = fopen("test.txt","w+");   if (flock($file,LOCK_EX))   {    fwrite($file,"Write something");    flock($file,LOCK_UN);   }   else   {     echo "Error locking file!";   }   fclose($file); ?>

Note:

The ghost file is exclusively locked (this file can only be read and written by this user). if other new users want to access this file, it will be blocked, until the former closes the file (release lock ).

2. if you change the code to flock ($ file, LOCK_EX + LOCK_NB), an Error is returned directly when the lock is triggered. if a new user accesses the file, an Error locking file is output!"

3. the syntax of this function is flock (file, lock, block), where file is required. Specifies the opened files to be locked or released. Lock is required. Specifies the type of lock to be used. Optional. If it is set to 1 or true, other processes are blocked when the lock is performed.

For example, write a PHP code to ensure that multiple processes write data to the same file at the same time.

function writeData($path, $mode,$data) {   $fp = fopen($path, $mode);    $retries = 0;   $max_retries = 100;    do{     if ($retries > 0)      {       usleep(rand(1, 10000));     }     $retries += 1;   }while (!flock($fp, LOCK_EX) and $retries<= $max_retries);    if ($retries == $max_retries)    {     return false;   }   fwrite($fp, "$data\n");   flock($fp, LOCK_UN);   fclose($fp);    return true;  }

I hope this article will help you with php programming.

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.