★ wanayoo — archive 1999 http://www.php.net/manual/de/function.flock.phpNouvelle recherche | Portail wanayoo

PHP Home Page

Manual Table of Contents
Up to Filesystem
Quick Reference
English version of this pageJapanese version of this pageItalian version of this pageFrench version of this pageHungarian version of this page
Filesystem
* basename
* chgrp
* chmod
* chown
* clearstatcache
* copy
* delete
* dirname
* diskfreespace
* fclose
* feof
* fgetc
* fgetcsv
* fgets
* fgetss
* file
* file_exists
* fileatime
* filectime
* filegroup
* fileinode
* filemtime
* fileowner
* fileperms
* filesize
* filetype
* flock
* fopen
* fpassthru
* fputs
* fread
* fseek
* ftell
* fwrite
* set_file_buffer
* is_dir
* is_executable
* is_file
* is_link
* is_readable
* is_writeable
* link
* linkinfo
* mkdir
* pclose
* popen
* readfile
* readlink
* rename
* rewind
* rmdir
* stat
* lstat
* realpath
* symlink
* tempnam
* touch
* umask
* unlink
Manual: flock
View the source code for this pageSearch the site



Previous page
 filetype
 Updated
Sat, 12 Aug 2000
fopen 
Next page


flock

(PHP3 >= 3.0.7, PHP4 )

flock -- Portables Datei-Verriegelungs-Verfahren

Beschreibung

bool flock (int fp, int operation)

PHP liefert einen portablen Weg, Dateien in einer "beratenden" Art und Weise zu verriegeln. D.h. alle zugreifende Programme müssen die gleiche Art und Weise der Verriegelung benutzen, oder es funktioniert nicht.

flock() arbeitet mit fp welcher ein existierender Dateizeiger sein muss. Der Parameter operation bestimmt die Art der Verriegelung und kann einen der folgenden Werte annehmen:

  • Um eine verteilte, nur lesende Verriegelung zu erhalten, setzten Sie operation auf 1.

  • Um eine exklusive, nur schreibende Verriegelung zu erhalten, setzen Sie operation auf 2.

  • Um eine Verriegelung freizugeben (lesend oder schreibend), setzten Sie operation auf 3.

  • Wenn Sie Zugriffe während der Verriegelung durch flock() erlauben wollen, fügen Sie 4 zu operation hinzu.

flock() erlaubt es Ihnen ein einfaches Leser/Schreiber Modell zu erstellen, welches auf fast jedem Betriebsystem benutzt werden kann (UNICES- und Windows- Systeme).

flock() gibt TRUE bei Erfolg, FALSE wenn ein Fehler auftrat (z.B. wenn eine Verriegelung nicht erstellt werden konnte) zurück.


User Contributed Notes: flock


t_pascal@zennet.com
08-Dec-1999 10:52
Anybody know if fclose() will remove the lock? I assume so, but there's nothing that says so. The only way is to test it, I guess. Also, will the exclusive lock prevent the same process from opening the file? Suppose I want to test the lock thusly:


<PRE>$fd = fopen("some.txt","r");
flock($fd,2); /* I want exclusive rights */
...
$fd2 = fopen("some.txt","r");
/* for whatever reason, maybe I'm testing the lock */
...
fclose($fd);
fclose($fd2);
</PRE>

??



chrish@turlyming.com
04-Feb-2000 12:43
If PHP makes a direct call to flock, you can use flock($fildes, 8) to unlock a file.


sterling@designmultimedia.com
05-Feb-2000 01:19
The lock on the file will be released once the file is closed (either by fclose or when the script stops executing). To manually release a lock go:

flock($fp, 3) or die("Cannot Release Lock");



hj@tringa.com
25-Mar-2000 04:31
I get the following, using PHP3 (3.0.5):

Fatal error: Call to unsupported or undefined function flock() in ...

This suggests that this function does not exist in all versions of PHP3, or is otherwise somehow limited in availability.



ryan@audiogalaxy.com
29-Mar-2000 10:40
Does anyone know if the lock holds across multiple web servers? For instance, say I have 10 webservers using an NFS mounted file to keep track of certain data. I want to lock the data for reading in PHP on one computer and update the file from another computer.


mpakula@gask.com.pl
06-Apr-2000 11:51
I want to make a page with a form for data collection. I want those data to be put into a text file. Can anyone tell me what's going to happen when two (or more) people will try to send the filled form at (almost) the same time? Will the data from different people be mixed? I made a small test and it seems to work OK (only one "proces" is writting to a file until it calls fclose()), but I don't like to rely on tests. After all - if it works without flock(), why would one want to use this function?


lpjweige@ihc.com
29-Apr-2000 12:54
Coming from the database world, I don't like how this is implemented. The main reason you would want to <B>lock</B> the file is to avoid multi-user <B>collisions <I>and</I> overwrites</B>. For example, this would be useless:

<PRE>
/* Open for writing only; place the file pointer at the
beginning of the file and <B>truncate</B> the file to zero length. */
$fp = fopen(myFile.dat, "w");
if (flock($fp, 2)) {
fwrite("my data");
}
flock($fp, 3);
</PRE>

The fopen is completed (<I>truncating the file</I>) <B>before</B> the flock() checks for a lock, unless fopen honors flock(). <I>(If it does, then what do they mean by "...which means all accessing programs have to use the same way of locking or it will not work"?)<I> Even so, though slim, if a second process is nearly syncronous and can execute fopen before the first can execute flock, your hosed. A better implementation would be to make flock like fopen with an additional arg -- int operation (as above) -- returning the file pointer on success, and FALSE otherwise. This way the file is locked when the connection is made.

It still leaves the possiblity of overwrites, but only if reading without exclusive locking. I welcome feedback. I will have to write my own file locking mechanism if fopen() doesn't honor flock().



paolo@mailgate.net
02-May-2000 05:51
In response to the very important issues above:

This kludge seems to work safely:

===Script 1 (reader)======
$fd = fopen('/tmp/php_flock_test', 'r');
flock($fd,1); #Read lock
sleep(60); #For testing purposes
$contents = fread ($fd, filesize('/tmp/php_flock_test') );
fclose($fd);
=====================

=====Script 2 (writer)==========
#Dummy initial filehandle to obtain lock via
$fd = fopen('/tmp/php_flock_test', 'a');

$locked = flock($fd,2); #Exclusive blocking lock
if (!$locked) {
print "Error: timeout obtaining lock"; exit;
}

#Second (actually used) filehandle
$fdsafe = fopen('/tmp/php_flock_test', 'w');
fwrite($fdsafe,'Replacement text');
fclose($fdsafe);

fclose($fd);
=================

The 'fake' append seems to do no harm - tested it and it works fine.



iaponte@cantv.net
06-Jul-2000 06:55

I use the function flock but it does not seems to work all the time. With a perl script I put my server under heavy stress and there was a time when the file reset it self. It was a file with 3 numbers. Like this


123

221

109

And when it gets to a high number it resetted mysteriously.



 About Notes


Previous page
 filetype
 Updated
Sat, 12 Aug 2000
fopen 
Next page





Who's responsible for this?
Top of this page

Site
Hosting:



Located in
United States
Elements of this website are subject to copyright.
Questions about installing or using PHP should be directed to one of the mailing lists.
Only questions about the website should be directed to webmaster@php.net.