downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

SplFileObject::fpassthru> <SplFileObject::fgetss
[edit] Last updated: Fri, 17 May 2013

view this page in

SplFileObject::flock

(PHP 5 >= 5.1.0)

SplFileObject::flockVerrouille ou déverrouille un fichier

Description

public bool SplFileObject::flock ( int $operation [, int &$wouldblock ] )

Verrouille ou déverrouille un fichier, de la même façon que la fonction flock().

Liste de paramètres

operation

Le paramètre operation peut prendre une des constantes suivantes :

  • LOCK_SH pour acquérir un verrou partagé (lecture).
  • LOCK_EX pour acquérir un verrou exclusif (écriture).
  • LOCK_UN pour déverrouiller le fichier (partagé ou exclusif).
  • LOCK_NB pour ne pas bloquer durant le verrouillage (non supporté sous Windows).

wouldblock

Définit à TRUE si le verrou doit être bloquant (condition pour l'errno EWOULDBLOCK).

Valeurs de retour

Cette fonction retourne TRUE en cas de succès ou FALSE si une erreur survient.

Exemples

Exemple #1 Exemple avec SplFileObject::flock()

<?php
$file 
= new SplFileObject("/tmp/lock.txt""w");
if (
$file->flock(LOCK_EX)) { // verrou exclusif
    
$file->ftruncate(0);     // coupe le fichier
    
$file->fwrite("Écrire quelque chose ici\n");
    
$file->flock(LOCK_UN);   // libère le verrou
} else {
    echo 
"Impossible de récupérer le verrou !";
}
?>

Voir aussi



add a note add a note User Contributed Notes SplFileObject::flock - [1 notes]
up
-1
digitalprecision at gmail dot com
2 years ago
For the record, the example given here has an explicit command to truncate the file, however with a 'write mode' of 'w', it will do this for you automatically, so the truncate call is not needed.

 
show source | credits | sitemap | contact | advertising | mirror sites