What is PhpDig?
PhpDig is a PHP MySQL based
Web Spider & Search Engine.



flock — Locks or releases a file.


bool flock(file_handle, operation);
resource file_handle: File handle
int operation: Lock type


TRUE on success; FALSE on error


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:

Operation Action
1 Acquire a shared lock (reader)
2 Acquire an exclusive lock (writer)
3 Release a lock
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.


PHP Version: 3.0.7 +


Example 321. Acquire an exclusive file lock

$fh = fopen("myfile", "r+");

if(flock($fh, 2))
    echo ("An exclusive lock has been acquired");
    die ("Lock couldn't be acquired");

/* perform safe read/write operations here */


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 The authors of this book have elected not to choose any options under the OPL. This online book was obtained from 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.

Powered by: vBulletin Version 3.0.7
Copyright ©2000 - 2005, Jelsoft Enterprises Ltd.
Copyright © 2001 - 2005, ThinkDing LLC. All Rights Reserved.