<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.filesystem.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'zh',
  ),
  'this' => 
  array (
    0 => 'function.flock.php',
    1 => 'flock',
    2 => '可移植的协同文件锁定',
  ),
  'up' => 
  array (
    0 => 'ref.filesystem.php',
    1 => '文件系统函数',
  ),
  'prev' => 
  array (
    0 => 'function.filetype.php',
    1 => 'filetype',
  ),
  'next' => 
  array (
    0 => 'function.fnmatch.php',
    1 => 'fnmatch',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'zh',
    'path' => 'reference/filesystem/functions/flock.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.flock" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">flock</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">flock</span> &mdash; <span class="dc-title">可移植的协同文件锁定</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.flock-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>flock</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span> <code class="parameter">$stream</code></span>, <span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$operation</code></span>, <span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter reference">&$would_block</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span>): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>flock()</strong></span> 允许执行简单的读取/写入模型,该模型可以在任何平台（包括大部分
   Unix 衍生产品，甚至是 Windows）上使用。
  </p>
  <p class="para">
   当调用 <span class="function"><a href="function.fclose.php" class="function">fclose()</a></span> 或者对 <code class="parameter">stream</code> 垃圾收集时，锁会释放。
  </p>
  <p class="para">
   PHP 支持以协同且可移植的方式锁定完整文件（也就是说所有访问程序必须使用相同锁定方式, 否则将无法工作）。默认情况下，该函数将堵塞直到获得锁；这可以通过下面文档中
   <strong><code><a href="filesystem.constants.php#constant.lock-nb">LOCK_NB</a></code></strong> 选项来控制。
  </p>
 </div>

 
 <div class="refsect1 parameters" id="refsect1-function.flock-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">stream</code></dt>
     <dd>
      <p class="para">文件系统指针，是典型地由
<span class="function"><a href="function.fopen.php" class="function">fopen()</a></span> 创建的 <span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span>(资源)。</p>
     </dd>
    
    
     <dt><code class="parameter">operation</code></dt>
     <dd>
      <p class="para">
       <code class="parameter">operation</code> 可以是以下值之一：
       <ul class="itemizedlist">
        <li class="listitem">
         <span class="simpara">
         <strong><code><a href="filesystem.constants.php#constant.lock-sh">LOCK_SH</a></code></strong>取得共享锁定（读取的程序）。
         </span>
        </li>
        <li class="listitem">
         <span class="simpara">
         <strong><code><a href="filesystem.constants.php#constant.lock-ex">LOCK_EX</a></code></strong> 取得独占锁定（写入的程序。
         </span>
        </li>
        <li class="listitem">
         <span class="simpara">
          <strong><code><a href="filesystem.constants.php#constant.lock-un">LOCK_UN</a></code></strong> 释放锁定（无论共享或独占）。
         </span>
        </li>
       </ul>
      </p>
      <p class="para">
       It is also possible to add <strong><code><a href="filesystem.constants.php#constant.lock-nb">LOCK_NB</a></code></strong> as a bitmask to one 
       of the above operations, if <span class="function"><strong>flock()</strong></span> should not
       block during the locking attempt.
      </p>
     </dd>
    
    
     <dt><code class="parameter">would_block</code></dt>
     <dd>
      <p class="para">
       如果锁将堵塞（EWOULDBLOCK
       错误码情况下），第三个可选参数会设置为 1。
      </p>
     </dd>
    
   </dl>
  </p>
 </div>

 
 <div class="refsect1 returnvalues" id="refsect1-function.flock-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>， 或者在失败时返回 <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.flock-examples">
  <h3 class="title">示例</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>示例 #1 <span class="function"><strong>flock()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$fp </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"/tmp/lock.txt"</span><span style="color: #007700">, </span><span style="color: #DD0000">"r+"</span><span style="color: #007700">);<br /><br />if (</span><span style="color: #0000BB">flock</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #0000BB">LOCK_EX</span><span style="color: #007700">)) {  </span><span style="color: #FF8000">// 进行排它型锁定<br />    </span><span style="color: #0000BB">ftruncate</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">);      </span><span style="color: #FF8000">// truncate file<br />    </span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #DD0000">"Write something here\n"</span><span style="color: #007700">);<br />    </span><span style="color: #0000BB">fflush</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);            </span><span style="color: #FF8000">// flush output before releasing the lock<br />    </span><span style="color: #0000BB">flock</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #0000BB">LOCK_UN</span><span style="color: #007700">);    </span><span style="color: #FF8000">// 释放锁定<br /></span><span style="color: #007700">} else {<br />    echo </span><span style="color: #DD0000">"Couldn't get the lock!"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-2">
    <p><strong>示例 #2 <span class="function"><strong>flock()</strong></span> 使用 <strong><code><a href="filesystem.constants.php#constant.lock-nb">LOCK_NB</a></code></strong> 选项</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$fp </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'/tmp/lock.txt'</span><span style="color: #007700">, </span><span style="color: #DD0000">'r+'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/* Activate the LOCK_NB option on an LOCK_EX operation */<br /></span><span style="color: #007700">if(!</span><span style="color: #0000BB">flock</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #0000BB">LOCK_EX </span><span style="color: #007700">| </span><span style="color: #0000BB">LOCK_NB</span><span style="color: #007700">)) {<br />    echo </span><span style="color: #DD0000">'Unable to obtain lock'</span><span style="color: #007700">;<br />    exit(-</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #FF8000">/* ... */<br /><br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
 </div>

 
 <div class="refsect1 notes" id="refsect1-function.flock-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    在 Windows 上，<span class="function"><strong>flock()</strong></span> 使用的是强制锁而不是协同锁。
    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.
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    由于 <span class="function"><strong>flock()</strong></span>
    需要一个文件指针， 因此可能不得不用一个特殊的锁定文件来保护打算通过写模式打开的文件的访问（在
    <span class="function"><a href="function.fopen.php" class="function">fopen()</a></span> 函数中加入 &quot;w&quot; 或 &quot;w+&quot;）。
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    只能用于 <span class="function"><a href="function.fopen.php" class="function">fopen()</a></span> 返回本地文件的文件指针，或指向实现
    <span class="function"><a href="streamwrapper.stream-lock.php" class="function">streamWrapper::stream_lock()</a></span> 方法的用户空间流的文件指针。
   </p>
  </p></blockquote>
  <div class="warning"><strong class="warning">警告</strong>
   <p class="para">
    在后续代码中为 <code class="parameter">stream</code> 分配另一个值将释放锁。
   </p>
  </div>
  <div class="warning"><strong class="warning">警告</strong>
   <p class="para">
    在部分操作系统中 <span class="function"><strong>flock()</strong></span> 以进程级实现。当用多线程服务器
    API 时，可能无法依靠 <span class="function"><strong>flock()</strong></span>
    来保护文件，因为运行于同一服务器实例中其它并行线程的 PHP 脚本可以对该文件进行处理！
   </p>
   <p class="para">
    旧的文件系统（如 <code class="literal">FAT</code> 及其衍生）不支持
    <span class="function"><strong>flock()</strong></span>，也因此在这些环境下总是返回 <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>。
   </p>
  </div>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    在 Windows 上，如果锁进程再次打开文件，那么在解锁文件之前，无法通过第二个句柄访问文件。
   </p>
  </p></blockquote>
 </div>


</div><?php manual_footer($setup); ?>