Filesystem 函数
PHP Manual

flock

(PHP 4, PHP 5)

flock轻便的咨询文件锁定

说明

bool flock ( int $handle , int $operation [, int &$wouldblock ] )

PHP 支持以咨询方式(也就是说所有访问程序必须使用同一方式锁定, 否则它不会工作)锁定全部文件的一种轻便方法。

Note:

在 Windows 下 flock() 将会强制执行。

flock() 操作的 handle 必须是一个已经打开的文件指针。operation 可以是以下值之一:

flock() 允许执行一个简单的可以在任何平台中使用的读取/写入模型(包括大部分的 Unix 派生版和甚至是 Windows)。如果锁定会堵塞的话(EWOULDBLOCK 错误码情况下),可选的第三个参数会被设置为 TRUE。锁定操作也可以被 fclose() 释放(代码执行完毕时也会自动调用)。

成功时返回 TRUE, 或者在失败时返回 FALSE.

Example #1 flock() 例子

<?php

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

if (
flock($fpLOCK_EX)) { // 进行排它型锁定
    
fwrite($fp"Write something here\n");
    
flock($fpLOCK_UN); // 释放锁定
} else {
    echo 
"Couldn't lock the file !";
}

fclose($fp);

?>

Note:

由于 flock() 需要一个文件指针, 因此可能不得不用一个特殊的锁定文件来保护打算通过写模式打开的文件的访问(在 fopen() 函数中加入 "w" 或 "w+")。

Warning

flock() 不能用于 NFS 以及其它一些网络文件系统。详细资料查看自己操作系统的文档。

在部分操作系统中 flock() 以进程级实现。当用一个多线程服务器 API(比如 ISAPI)时,可能不可以依靠 flock() 来保护文件,因为运行于同一服务器实例中其它并行线程的 PHP 脚本可以对该文件进行处理。

flock() 不支持旧的文件系统,如 FAT 以及它的派生系统。因此,此环境下总是返回 FALSE(尤其是对 Windows 98 用户来说)。

参数

handle

文件系统指针,是典型地由 fopen() 创建的 resource(资源)。

operation

operation is one of the following:

  • LOCK_SH to acquire a shared lock (reader).
  • LOCK_EX to acquire an exclusive lock (writer).
  • LOCK_UN to release a lock (shared or exclusive).

It is also possible to add LOCK_NB as a bitmask to one of the above operations if you don't want flock() to block while locking. (not supported on Windows)

wouldblock

The optional third argument is set to TRUE if the lock would block (EWOULDBLOCK errno condition). (not supported on Windows)

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE.

更新日志

版本 说明
5.3.2 The automatic unlocking when the file's resource handle is closed was removed. Unlocking now always has to be done manually.
4.0.1 The LOCK_XXX constants were added. Prior to that you must use 1 for LOCK_SH, 2 for LOCK_EX, 3 for LOCK_UN and 4 for LOCK_NB

范例

Example #2 flock() example

<?php

$fp 
fopen("/tmp/lock.txt""r+");

if (
flock($fpLOCK_EX)) {  // acquire an exclusive lock
    
ftruncate($fp0);      // truncate file
    
fwrite($fp"Write something here\n");
    
fflush($fp);            // flush output before releasing the lock
    
flock($fpLOCK_UN);    // release the lock
} else {
    echo 
"Couldn't get the lock!";
}

fclose($fp);

?>

Example #3 flock() using the LOCK_NB option

<?php
$fp 
fopen('/tmp/lock.txt''r+');

/* Activate the LOCK_NB option on an LOCK_EX operation */
if(!flock($fpLOCK_EX LOCK_NB)) {
    echo 
'Unable to obtain lock';
    exit(-
1);
}

/* ... */

fclose($fp);
?>

注释

Note:

flock() uses mandatory locking instead of advisory locking on Windows. Mandatory locking is also supported on Linux and System V based operating systems via the usual mechanism supported by the fcntl() system call: that is, if the file in question has the setgid permission bit set and the group execution bit cleared. On Linux, the file system will also need to be mounted with the mand option for this to work.

Note:

Because flock() requires a file pointer, you may have to use a special lock file to protect access to a file that you intend to truncate by opening it in write mode (with a "w" or "w+" argument to fopen()).

Note:

May only be used on file pointers returned by fopen() for local files, or file pointers pointing to userspace streams that implement the streamWrapper::stream_lock() method.

Warning

Assigning another value to handle argument in subsequent code will release the lock.

Warning

On some operating systems flock() is implemented at the process level. When using a multithreaded server API like ISAPI you may not be able to rely on flock() to protect files against other PHP scripts running in parallel threads of the same server instance!

flock() is not supported on antiquated filesystems like FAT and its derivates and will therefore always return FALSE under this environments (this is especially true for Windows 98 users).


Filesystem 函数
PHP Manual