Home > Backend Development > PHP Tutorial > How to use file lock in PHP_PHP tutorial

How to use file lock in PHP_PHP tutorial

WBOY
Release: 2016-07-13 17:42:01
Original
1060 people have browsed it

//WRITE
$fp = fopen("test.txt", ab); //From the end
flock($fp, LOCK_EX);  fwrite($fp, Just A Test String.....); //Start writing...
flock($fp, LOCK_UN);  fclose($fp);                                                                                                                                                  
//READ
$fp = fopen("test.txt", r);

flock($fp, LOCK_SH);

//Read from the file......
flock($fp, LOCK_UN);
fclose($fp); Let’s take a detailed look at the introduction of the function flock in the PHP manual:

flock -- lightweight consultation file locking

The function prototype is: bool flock (int handle, int operation [, int &wouldblock])

PHP supports a lightweight method of locking all files in an advisory manner (that is, all accessing programs must lock in the same way, otherwise it will not work). Something to note is: flock() under Windows will Enforcement. The handle of the flock() operation must be an open file pointer. operation can be one of the following values:

To obtain a shared lock (reading program), set operation to LOCK_SH (set to 1 in versions prior to PHP 4.0.1).

To obtain an exclusive lock (writing program), set operation to LOCK_EX (set to 2 in versions prior to PHP 4.0.1).

To release a lock (whether shared or exclusive), set operation to LOCK_UN (set to 3 in versions prior to PHP 4.0.1).

If you do not want flock() to block when locked, add LOCK_NB to the operation (set to 4 in versions prior to PHP 4.0.1). flock() allows the implementation of a simple read/write model that can be used on any platform (including most Unix derivatives and even Windows). The optional third parameter is set to TRUE if the lock would block (in case of EWOULDBLOCK error code). Lock operations can also be released by fclose() (also called automatically when the code completes execution). Returns TRUE on success, FALSE on failure.

Let’s take a look at the classic example in the PHP manual:

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

if (flock($fp, LOCK_EX)) { // Perform exclusive lock

fwrite($fp, "Write something here ");

flock($fp, LOCK_UN); // Release lock

} else {
echo "Couldn't lock the file !";
}

fclose($fp);

?> Note: Since flock() requires a file pointer, you may have to use a special lock file to protect access to files intended to be opened in write mode (add "w" or " w+").

Note:

flock() cannot be used with NFS and some other network file systems. Check your operating system's documentation for details.

In some operating systems, flock() is implemented at the process level. When using a multi-threaded server API (such as ISAPI), it may not be possible to rely on flock() to protect the file, because the file can be processed by PHP scripts running in other parallel threads in the same server instance.

flock() does not support older file systems such as FAT and its derivatives. Therefore, FALSE is always returned in this environment (especially on Windows

http://www.bkjia.com/PHPjc/486108.html

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486108.htmlTechArticle//WRITE $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)...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template