6.8 Locking Files


You want to make sure that, when you are accessing a file, no other programs or processes are accessing that same file.

Technique

Use PHP's flock () function:

 Lockfile.inc <?php function lock ($fp, $lock_level=LOCK_EX) {     @flock ($fp, $lock_level)       or die ("Cannot flock filepointer to $lock_level"); } function unlock ($fp) {     @flock ($fp, LOCK_UN)       or die ("Cannot Release the Lock"); } ?> 

Then use these wrappers as follows :

 <?php include("Lockfile.inc"); $filename = "/home/designmm/www/index.df"; $fp = @fopen($filename, "w")        or die("Cannot Open $filename for write access"); // Always exclusively lock a file when you are writing to it // this is the same as flock($fp, LOCK_EX); lock ($fp); fwrite ($fp, "Hello"); // Unlock the file, this is the same as flock($fp, LOCK_UN); unlock ($fp); @fclose ($fp); ?> 

Comments

When accessing files concurrently ( especially for write access), there is a very real possibility of your files becoming corrupt. The solution to this is flock() . flock() takes a file pointer and sets a certain access level for that file. PHP supports a portable way to lock complete files in an advisory way (which means that all accessing programs have to use the same way of locking or it will not work).

Let me further explain: The flock() function is sort of like a crossing guard. It tells the different cooperating processes when they can and cannot access the files in different modes (shared and exclusive). A file may have multiple concurrent shared locks, meaning that many processes may have a shared lock on the file at the same time. However, an exclusive lock (which should be used when you write to files), allows only one user at a time to have a lock on the file. That means other processes requesting shared locks or exclusive locks have to wait until the already existing exclusive lock is released before they can have access to the file.

However, this will affect only programs that implement portable file advisory locking. If another process does not respect or recognize the lock that flock() has put on your file, that process will simply ignore your existing lock. The bottom line is that for flock() to work, all programs modifying the file must respect flock() .

flock() uses three codes to specify lock operations: LOCK_SH to specify a shared lock, LOCK_EX to specify an exclusive lock, and LOCK_UN to release the lock. If you do not want flock() to block while it acquires a lock, add LOCK_NB to the code you are using. So, creating a shared lock would be LOCK_SH + LOCK_NB , creating an exclusive lock would be LOCK_EX + LOCK_NB , and releasing a lock would be LOCK_UN + LOCK_NB .

You must tell your program to die if flock() does not work, otherwise your program will hang until flock() locks. To accomplish this, you can do the following:

 <?php $fp = @fopen ($fn, "r")   or die("Cannot open $fn"); @flock ($fp, LOCK_SH + LOCK_NB)   or die ("Cannot Get Shared lock"); @fclose($fp)   or die ("Cannot close $fn"); ?> 


PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net