PHP use flock to implement file lock _ PHP Tutorial

Source: Internet
Author: User
Tags flock
PHP uses flock to implement file locking. PHP uses flock to implement file lock. This article describes how PHP uses flock to implement file lock. Share it with you for your reference. The specific analysis is as follows: flock in the official document PHP uses flock to implement file locking

This article describes how PHP uses flock to implement file locking. 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:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

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.

Examples in this article describes how PHP uses flock to implement file locking. Share it with you for your reference. The specific analysis is as follows: flock in the official documentation...

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.