PHP using flock to implement file Lock method _php Tutorial

Source: Internet
Author: User
Tags flock

PHP uses flock to implement file lock method


The example in this paper describes how PHP uses flock to implement file locking. Share to everyone for your reference. The specific analysis is as follows:

Flock's explanation in the official documentation is that 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.

In simple terms, locking a file allows multiple processes to restrict access to the file, preventing collisions. As an 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);

?>

Description

1. This code means to open the file Test.txt read-write, when a user calls the PHP page, that is, the Test.txt file operation, then the Flock ($file, LOCK_EX) code is executed, the Test.txt file will be exclusive lock ( The file can only be read and written by that user, and if any other new user wants to access the file, it will be blocked until the file is closed (releasing the lock).

2. If the code is changed to flock ($file, LOCK_EX+LOCK_NB) to indicate that the error is returned directly when locked, then the "Error locking file!" will be output if a new user accesses the file

3. The syntax of the function is flock (file,lock,block), where file is required. Specifies the open files to lock or release. Lock required. Specifies which type of lock to use. Block is optional. If set to 1 or true, the other process is blocked when the lock is made.

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

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 is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/1025313.html www.bkjia.com true http://www.bkjia.com/PHPjc/1025313.html techarticle PHP using flock to implement file lock method in this paper, we describe the way PHP uses flock to implement file lock. Share to everyone for your reference. The specific analysis is as follows: Flock in official documents ...

  • Related Article

    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.