13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

78 Chapter 2 Storing <strong>and</strong> Retrieving Data<br />

Locking Files<br />

Imagine a situation in which two customers are trying to order a product at the same<br />

time. (This situation is not uncommon, especially when your website starts to get any<br />

kind of traffic volume.) What if one customer calls fopen() <strong>and</strong> begins writing, <strong>and</strong> then<br />

the other customer calls fopen() <strong>and</strong> also begins writing? What will be the final contents<br />

of the file? Will it be the first order followed by the second order, or vice versa?<br />

Will it be one order or the other? Or will it be something less useful, such as the two<br />

orders interleaved somehow? The answer depends on your operating system but is often<br />

impossible to know.<br />

To avoid problems like this, you can use file locking.You use this feature in <strong>PHP</strong> by<br />

using the flock() function.This function should be called after a file has been opened<br />

but before any data is read from or written to the file.<br />

The prototype for flock() is<br />

bool flock (resource fp, int operation [, int &wouldblock])<br />

You need to pass it a pointer to an open file <strong>and</strong> a constant representing the kind of lock<br />

you require. It returns true if the lock was successfully acquired <strong>and</strong> false if it was not.<br />

The optional third parameter will contain the value true if acquiring the lock would<br />

cause the current process to block (that is, have to wait).<br />

The possible values for operation are shown in Table 2.2.The possible values<br />

changed at <strong>PHP</strong> 4.0.1, so both sets of values are shown in the table.<br />

Table 2.2 flock() Operation Values<br />

Value of Operation Meaning<br />

LOCK_SH (formerly 1)<br />

LOCK_EX (formerly 2)<br />

LOCK_UN (formerly 3)<br />

LOCK_NB (formerly 4)<br />

Reading lock.The file can be shared with other readers.<br />

Writing lock.This operation is exclusive; the file cannot be shared.<br />

The existing lock is released.<br />

Blocking is prevented while you are trying to acquire a lock.<br />

If you are going to use flock(), you need to add it to all the scripts that use the file;<br />

otherwise, it is worthless.<br />

Note that flock() does not work with NFS or other networked file systems. It also<br />

does not work with older file systems that do not support locking, such as FAT. On<br />

some operating systems, it is implemented at the process level <strong>and</strong> does not work correctly<br />

if you are using a multithreaded server API.<br />

To use it with the order example, you can alter processorder.php as follows:<br />

$fp = fopen(“$DOCUMENT_ROOT/../orders/orders.txt”, ‘ab’);<br />

flock($fp, LOCK_EX); // lock the file for writing<br />

fwrite($fp, $outputstring);<br />

flock($fp, LOCK_UN); // release write lock<br />

fclose($fp);

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!