|
Posted by paul on 09/06/05 02:00
Hi,
If more than one user clicks the submit button at the same time can I use
flock to ensure that all the data gets written to the file? Is the following
code correct?
Thanks,
Paul
=======================
// open for writing
$fp = fopen($filename, "a") or die("Unable to open file, please try again
later.");
// wait for unlock if it's busy
while(TRUE) // do forever
{
if( flock($fp, LOCK_EX) )
{
// write to file
fwrite($fp, $first);
fwrite($fp, $last);
fwrite($fp, $phone);
// unlock file
flock($fp, LOCK_UN);
break; // done, get out of loop
}
sleep(1); // wait a second and try again
}
// close file
fclose($fp);
============================
[Back to original message]
|