PHP multiple processes write to the same file at the same time

Source: Internet
Author: User
Tags flock

Flock

(PHP 3 >= 3.0.7, PHP 4, PHP 5)

Flock--Light consultation document locking instructions BOOL Flock(int handle, int operation [, int &wouldblock])

PHP supports a lightweight method of locking all files in a consultative manner (that is, all access programs must be locked in the same way, otherwise it will not work).

Note: under Windows flock () will be enforced.

The flock () operation handle must be a file pointer that has already been opened. operationcan be one of the following values:

1. to get a shared lock (reader), set to operation lock_sh(Previous version of PHP 4.0.1 to 1).

2. to obtain an exclusive lock (write program), set to operation lock_ex(2 in previous versions of PHP 4.0.1).

3. to release the lock (either shared or exclusive), set to operation Lock_un(3 in previous versions of PHP 4.0.1).

4. If you do not want flock () to block when locked, operation add lock_nb(set to 4 in the previous version of PHP 4.0.1).

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

Returns TRUEif successful, and FALSEif it fails.

Example 1. flock () Example

<?PHP$fp=fopen("/tmp/lock.txt", "w+");if(Flock($fp, LOCK_EX)) {//make an exclusive 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 need a lock file to protect access to the files that you intend to truncate by opening in write mode (add "w" or "w+" in the fopen () function).

Warning:

flock () cannot work correctly in NFS and some other network file systems. Please check your operating system documentation for more information.

In some operating systems,flock () is executed at the processing level. 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 threads within the same server can process the file.

flock () does not support legacy file systems, such as FAT as well as its derived systems. Therefore, it often returns a FALSE value in this case (especially for Windows 98 users).

For example: Write a PHP code to ensure that multiple processes write to the same file successfully

functionWriteData ($path,$mode,$data,$max _retries= 10)  {      $fp=fopen($path,$mode); $retries= 0;  Do{         if($retries> 0)          {              Usleep(Rand(1, 10000)); }         $retries+ = 1; } while(!Flock($fp, LOCK_EX) and$retries<=$max _retries); //determines whether the maximum number of retries is equal to the return false    if($retries==$max _retries)       {         return false; }      fwrite($fp, "$data"); Flock($fp,Lock_un); fclose($fp); return true; }

This article turns from http://www.xuejiehome.com/blread-1670.html

PHP multiple processes write to the same file at the same time

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.