📄 function.flock.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Portable advisory file locking</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.filetype.html">filetype</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.fnmatch.html">fnmatch</a></div> <div class="up"><a href="ref.filesystem.html">Filesystem Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="function.flock" class="refentry"> <div class="refnamediv"> <h1 class="refname">flock</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">flock</span> — <span class="dc-title">Portable advisory file locking</span></p> </div> <div class="refsect1 description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">bool</span> <span class="methodname"><b><b>flock</b></b></span> ( <span class="methodparam"><span class="type">resource</span> <tt class="parameter">$handle</tt></span> , <span class="methodparam"><span class="type">int</span> <tt class="parameter">$operation</tt></span> [, <span class="methodparam"><span class="type">int</span> <tt class="parameter reference">&$wouldblock</tt></span> ] )</div> <p class="para rdfs-comment"> <b>flock()</b> 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 <a href="function.fclose.html" class="function">fclose()</a> (which is also called automatically when script finished). </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). </p> </div> <div class="refsect1 parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><i><tt class="parameter">handle</tt></i></span> <dd> <p class="para"> An open file pointer. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">operation</tt></i></span> <dd> <p class="para"> <i><tt class="parameter">operation</tt></i> is one of the following: <ul class="itemizedlist"> <li class="listitem"> <span class="simpara"> <b><tt>LOCK_SH</tt></b> to acquire a shared lock (reader). </span> </li> <li class="listitem"> <span class="simpara"> <b><tt>LOCK_EX</tt></b> to acquire an exclusive lock (writer). </span> </li> <li class="listitem"> <span class="simpara"> <b><tt>LOCK_UN</tt></b> to release a lock (shared or exclusive). </span> </li> <li class="listitem"> <span class="simpara"> <b><tt>LOCK_NB</tt></b> if you don't want <b>flock()</b> to block while locking. (not supported on Windows) </span> </li> </ul> </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">wouldblock</tt></i></span> <dd> <p class="para"> The optional third argument is set to <b><tt>TRUE</tt></b> if the lock would block (EWOULDBLOCK errno condition). </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns <b><tt>TRUE</tt></b> on success or <b><tt>FALSE</tt></b> on failure. </p> </div> <div class="refsect1 changelog"> <h3 class="title">ChangeLog</h3> <p class="para"> <table class="informaltable"> <colgroup> <thead valign="middle"> <tr valign="middle"> <th colspan="1">Version</th> <th colspan="1">Description</th> </tr> </thead> <tbody valign="middle" class="tbody"> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">4.0.1</td> <td colspan="1" rowspan="1" align="left"> The <i>LOCK_XXX</i> constants were added. Prior to that you must use 1 for <b><tt>LOCK_SH</tt></b>, 2 for <b><tt>LOCK_EX</tt></b>, 3 for <b><tt>LOCK_UN</tt></b> and 4 for <b><tt>LOCK_NB</tt></b> </td> </tr> </tbody> </colgroup> </table> </p> </div> <div class="refsect1 examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example"> <p><b>Example #1 <b>flock()</b> example</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?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">"w+"</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">// do an exclusive lock<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">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 lock the file !"</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">?></span></span></code></div> </div> </div> </p> </div> <div class="refsect1 notes"> <h3 class="title">Notes</h3> <blockquote><p><b class="note">Note</b>: <b>flock()</b> locks mandatory under Windows. <br /> </p></blockquote> <blockquote><p><b class="note">Note</b>: Because <b>flock()</b> 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 <a href="function.fopen.html" class="function">fopen()</a>). <br /> </p></blockquote> <div class="warning"><b class="warning">Warning</b> <p class="para"> <b>flock()</b> will not work on NFS and many other networked file systems. Check your operating system documentation for more details. </p> <p class="para"> On some operating systems <b>flock()</b> is implemented at the process level. When using a multithreaded server API like ISAPI you may not be able to rely on <b>flock()</b> to protect files against other PHP scripts running in parallel threads of the same server instance! </p> <p class="para"> <b>flock()</b> is not supported on antiquated filesystems like <i>FAT</i> and its derivates and will therefore always return <b><tt>FALSE</tt></b> under this environments (this is especially true for Windows 98 users). </p> </div> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.filetype.html">filetype</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.fnmatch.html">fnmatch</a></div> <div class="up"><a href="ref.filesystem.html">Filesystem Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -