flock
(PHP 3>= 3.0.7, PHP 4 )flock -- Portable advisory file locking
Description
bool flock ( resource handle, int operation [, int &wouldblock])
PHP supports a portable way of locking complete files in an advisory way (which means all accessing programs have to use the same way of locking or it will not work). flock() operates on handle which must be an open file pointer. operation is one of the following values: To acquire a shared lock (reader), set operation to LOCK_SH (set to 1 prior to PHP 4.0.1). To acquire an exclusive lock (writer), set operation to LOCK_EX (set to 2 prior to PHP 4.0.1). To release a lock (shared or exclusive), set operation to LOCK_UN (set to 3 prior to PHP 4.0.1). If you don't want flock() to block while locking, add LOCK_NB (4 prior to PHP 4.0.1) to operation. 
flock() allows you to perform a simple reader/writer model which can be used on virtually every platform (including most Unix derivatives and even Windows). The optional third argument is set to TRUE if the lock would block (EWOULDBLOCK errno condition) Returns TRUE on success or FALSE on failure. Example 1. flock() example<?php$fp = fopen("/tmp/lock.txt", "w+");if (flock($fp, LOCK_EX)) { // do an exclusive lock
    fwrite($fp, "Write something here\n");
    flock($fp, LOCK_UN); // release the lock
} else {
    echo "Couldn't lock the file !";
}fclose($fp);?>
 
 

解决方案 »

  1.   

    bool flock ( resource handle, int operation [, int wouldblock])锁定有handle指定的文件的文件路径。参数operation可能是下面的值之一:LOCK_SH  共享锁(读进程)
    LOCK_EX  独占锁(写进程)
    LOCK_UN  解除锁(独占的或共享的)
    LOCK_NB 加到LOCK_SHLO或CK_EX上以获取没有锁定的块如果wouldblock设定为true,操作将导致块在文件上堵塞。如果不能锁定,函数返回false,如果成功则返回true.