<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML ><HEAD ><TITLE >flock</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="Manual PHP" HREF="index.html"><LINK REL="UP" TITLE="Filesystem Functions" HREF="ref.filesystem.html"><LINK REL="PREVIOUS" TITLE="filetype" HREF="function.filetype.html"><LINK REL="NEXT" TITLE="fnmatch" HREF="function.fnmatch.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-2"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >Manual PHP</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.filetype.html" ACCESSKEY="P" >Înapoi</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.fnmatch.html" ACCESSKEY="N" >Înainte</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.flock" ></A >flock</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN28250" ></A ><P > (PHP 3>= 3.0.7, PHP 4 , PHP 5)</P >flock -- Portable advisory file locking</DIV ><DIV CLASS="refsect1" ><A NAME="AEN28253" ></A ><H2 >Description</H2 >bool <B CLASS="methodname" >flock</B > ( resource handle, int operation [, int &wouldblock])<BR ></BR ><P > 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 CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Notã: </B > <B CLASS="function" >flock()</B > is mandatory under Windows. </P ></BLOCKQUOTE ></DIV ><P > <B CLASS="function" >flock()</B > operates on <VAR CLASS="parameter" >handle</VAR > which must be an open file pointer. <VAR CLASS="parameter" >operation</VAR > is one of the following values: </P ><P > <P ></P ><UL ><LI ><P > To acquire a shared lock (reader), set <VAR CLASS="parameter" >operation</VAR > to <TT CLASS="constant" ><B >LOCK_SH</B ></TT > (set to 1 prior to PHP 4.0.1). </P ></LI ><LI ><P > To acquire an exclusive lock (writer), set <VAR CLASS="parameter" >operation</VAR > to <TT CLASS="constant" ><B >LOCK_EX</B ></TT > (set to 2 prior to PHP 4.0.1). </P ></LI ><LI ><P > To release a lock (shared or exclusive), set <VAR CLASS="parameter" >operation</VAR > to <TT CLASS="constant" ><B >LOCK_UN</B ></TT > (set to 3 prior to PHP 4.0.1). </P ></LI ><LI ><P > If you don't want <B CLASS="function" >flock()</B > to block while locking, add <TT CLASS="constant" ><B >LOCK_NB</B ></TT > (4 prior to PHP 4.0.1) to <VAR CLASS="parameter" >operation</VAR >. </P ></LI ></UL > </P ><P > <B CLASS="function" >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). The optional third argument is set to <TT CLASS="constant" ><B >TRUE</B ></TT > if the lock would block (EWOULDBLOCK errno condition) </P ><P > Returneazã <TT CLASS="constant" ><B >TRUE</B ></TT > în caz de succes, <TT CLASS="constant" ><B >FALSE</B ></TT > în caz contrar. </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN28301" ></A ><P ><B >Exemplu 1. <B CLASS="function" >flock()</B > example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /><br />$fp </font><font color="#007700">= </font><font color="#0000BB">fopen</font><font color="#007700">(</font><font color="#DD0000">"/tmp/lock.txt"</font><font color="#007700">, </font><font color="#DD0000">"w+"</font><font color="#007700">);<br /><br />if (</font><font color="#0000BB">flock</font><font color="#007700">(</font><font color="#0000BB">$fp</font><font color="#007700">, </font><font color="#0000BB">LOCK_EX</font><font color="#007700">)) { </font><font color="#FF8000">// do an exclusive lock<br /> </font><font color="#0000BB">fwrite</font><font color="#007700">(</font><font color="#0000BB">$fp</font><font color="#007700">, </font><font color="#DD0000">"Write something here\n"</font><font color="#007700">);<br /> </font><font color="#0000BB">flock</font><font color="#007700">(</font><font color="#0000BB">$fp</font><font color="#007700">, </font><font color="#0000BB">LOCK_UN</font><font color="#007700">); </font><font color="#FF8000">// release the lock<br /></font><font color="#007700">} else {<br /> echo </font><font color="#DD0000">"Couldn't lock the file !"</font><font color="#007700">;<br />}<br /><br /></font><font color="#0000BB">fclose</font><font color="#007700">(</font><font color="#0000BB">$fp</font><font color="#007700">);<br /><br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Notã: </B > Because <B CLASS="function" >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" ><B CLASS="function" >fopen()</B ></A >). </P ></BLOCKQUOTE ></DIV ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Avertisment</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P > <B CLASS="function" >flock()</B > will not work on NFS and many other networked file systems. Check your operating system documentation for more details. </P ><P > On some operating systems <B CLASS="function" >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 CLASS="function" >flock()</B > to protect files against other PHP scripts running in parallel threads of the same server instance! </P ><P > <B CLASS="function" >flock()</B > is not supported on antiquated filesystems like <VAR CLASS="literal" >FAT</VAR > and its derivates and will therefore always return <TT CLASS="constant" ><B >FALSE</B ></TT > under this environments (this is especially true for Windows 98 users). </P ></TD ></TR ></TABLE ></DIV ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.filetype.html" ACCESSKEY="P" >Înapoi</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Acasã</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.fnmatch.html" ACCESSKEY="N" >Înainte</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >filetype</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.filesystem.html" ACCESSKEY="U" >Sus</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >fnmatch</TD ></TR ></TABLE ></DIV ></BODY ></HTML >