<?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 => 'en',
  ),
  'this' => 
  array (
    0 => 'function.flock.php',
    1 => 'flock',
    2 => 'Portable advisory file locking',
  ),
  'up' => 
  array (
    0 => 'ref.filesystem.php',
    1 => 'Filesystem Functions',
  ),
  'prev' => 
  array (
    0 => 'function.filetype.php',
    1 => 'filetype',
  ),
  'next' => 
  array (
    0 => 'function.fnmatch.php',
    1 => 'fnmatch',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    '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">Portable advisory file locking</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.flock-description">
  <h3 class="title">Description</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> allows you to perform a simple reader/writer
   model which can be used on virtually every platform (including most Unix
   derivatives and even Windows).
  </p>
  <p class="para">
   The lock is released also by <span class="function"><a href="function.fclose.php" class="function">fclose()</a></span>,
   or when <code class="parameter">stream</code> is garbage collected.
  </p>
  <p class="para">
   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). By default, this function will block until the
   requested lock is acquired; this may be controlled with the <strong><code><a href="filesystem.constants.php#constant.lock-nb">LOCK_NB</a></code></strong> option documented below.
  </p>
 </div>

 
 <div class="refsect1 parameters" id="refsect1-function.flock-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">stream</code></dt>
     <dd>
      <p class="para">A file system pointer <span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span>
that is typically created using <span class="function"><a href="function.fopen.php" class="function">fopen()</a></span>.</p>
     </dd>
    
    
     <dt><code class="parameter">operation</code></dt>
     <dd>
      <p class="para">
       <code class="parameter">operation</code> is one of the following:
       <ul class="itemizedlist">
        <li class="listitem">
         <span class="simpara">
          <strong><code><a href="filesystem.constants.php#constant.lock-sh">LOCK_SH</a></code></strong> to acquire a shared lock (reader).
         </span>
        </li>
        <li class="listitem">
         <span class="simpara">
          <strong><code><a href="filesystem.constants.php#constant.lock-ex">LOCK_EX</a></code></strong> to acquire an exclusive lock (writer).
         </span>
        </li>
        <li class="listitem">
         <span class="simpara">
          <strong><code><a href="filesystem.constants.php#constant.lock-un">LOCK_UN</a></code></strong> to release a lock (shared or exclusive).
         </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">
       The optional third argument is set to 1 if the lock would block
       (EWOULDBLOCK errno condition).
      </p>
     </dd>
    
   </dl>
  </p>
 </div>

 
 <div class="refsect1 returnvalues" id="refsect1-function.flock-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   Returns <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> on success or <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> on failure.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.flock-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Example #1 <span class="function"><strong>flock()</strong></span> example</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">// acquire an exclusive lock<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">// release the lock<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>Example #2 <span class="function"><strong>flock()</strong></span> using the <strong><code><a href="filesystem.constants.php#constant.lock-nb">LOCK_NB</a></code></strong> option</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">Notes</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    <span class="function"><strong>flock()</strong></span> 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.
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    Because <span class="function"><strong>flock()</strong></span> 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 &quot;w&quot; or &quot;w+&quot; argument to
    <span class="function"><a href="function.fopen.php" class="function">fopen()</a></span>).
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    May only be used on file pointers returned by <span class="function"><a href="function.fopen.php" class="function">fopen()</a></span>
    for local files, or file pointers pointing to userspace streams that
    implement the <span class="function"><a href="streamwrapper.stream-lock.php" class="function">streamWrapper::stream_lock()</a></span> method.
   </p>
  </p></blockquote>
  <div class="warning"><strong class="warning">Warning</strong>
   <p class="para">
    Assigning another value to <code class="parameter">stream</code> argument in
    subsequent code will release the lock.
   </p>
  </div>
  <div class="warning"><strong class="warning">Warning</strong>
   <p class="para">
    On some operating systems <span class="function"><strong>flock()</strong></span> is implemented at
    the process level. When using a multithreaded server API you
    may not be able to rely on <span class="function"><strong>flock()</strong></span> to protect files
    against other PHP scripts running in parallel threads of the same server
    instance!
   </p>
   <p class="para">
    <span class="function"><strong>flock()</strong></span> is not supported on antiquated filesystems like
    <code class="literal">FAT</code> and its derivates and will therefore always
    return <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> under these environments.
   </p>
  </div>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    On Windows, if the locking process opens the file a second time, it cannot
    access the file through this second handle until it unlocks the file.
   </p>
  </p></blockquote>
 </div>

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