PHP flock() Funktion
❮ Referenz zum PHP-DateisystemBeispiel
Datei sperren und freigeben:
<?php
$file = fopen("test.txt","w+");
// exclusive lock
if (flock($file,LOCK_EX)) {
fwrite($file,"Add some text to the file.");
fflush($file);
// release lock
flock($file,LOCK_UN);
}
else {
echo "Error locking file!";
}
fclose($file);
?>
Definition und Verwendung
Die Funktion flock() sperrt und gibt eine Datei frei.
Syntax
flock(file, lock, block)
Parameterwerte
Parameter | Description |
---|---|
file | Required. Specifies an open file to lock or release |
lock | Required. Specifies what kind of lock to use. Possible values:
|
block | Optional. Set to 1 to block other processes while locking |
Technische Details
Rückgabewert: | TRUE bei Erfolg, FALSE bei Misserfolg |
---|---|
PHP-Version: | 4.0+ |
PHP-Änderungsprotokoll: | PHP 5.5: Unterstützung für den Blockparameter unter Windows hinzugefügt. PHP 5.3: Automatisches Entsperren bei fclose() entfernt. Das Entsperren muss nun manuell erfolgen |
❮ Referenz zum PHP-Dateisystem