flock
Name
flock — Locks or releases a file.
Synopsis
bool flock
(file_handle, operation);
resource file_handle: File
handle
int operation: Lock
type
Returns
TRUE on success; FALSE on error
Description
This function can put a lock on a file or release a
previously established lock. Depending on the value of
the operation, you have the following options:
A shared lock is a so-called
reader lockthat should
be used if you intend only to read from a file. This
ensures that the file remains intact for reading while
still allowing other processes to access the file. An
exclusive lock is a
writer lock that should be used
if you intend to change a file. This type of lock prevents
other processes from accessing the file. Both lock types
can be released with an operation value of 3. If you don't
want
flock() to wait until the desired lock type can
be acquired, add the value
4to your operation
parameter.
These locks only apply to the current PHP process. Any
other process can modify or delete a PHP-locked file if
permissions allow. Also note that multithreaded servers
such as IIS may not correctly handle files locked by
flock() . Other threads in the same server
instance may be able to modify the files, regardless of
file locking.
Version
PHP Version: 3.0.7 +
Example
Example 321. Acquire an
exclusive file lock
$fh = fopen("myfile", "r+");
if(flock($fh, 2))
echo ("An exclusive lock has been acquired");
else
die ("Lock couldn't be acquired");
/* perform safe read/write operations here */
fclose($fh);
PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing
(Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch).
This material may be distributed only subject to the terms and conditions set forth
in the Open Publication License, v1.0 or later (the latest version is presently available at
http://www.opencontent.org/openpub/).
The authors of this book have elected not to choose any options under the OPL. This online book was obtained
from
http://www.fooassociates.com/phpfer/
and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4
for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All
persons and entities shall have neither liability nor responsibility to any person or entity with respect to
any loss or damage arising from the information contained in this book.