Karl Hungus wrote:
I have a question about the behavior of a text file thats being written and
read to.
(snip) Is there any risk in this approach? While the XML file is being written to,
what is its readabililty? And vise-versa. Assuming Im using the standard
read write functions for files in PHP, will the read wait for a write to
finish, etc.
I think you should use the file-locking mechanism to avoid risk.
http://www.php.net/flock
<?php
$xml = '/path/to/xmlfile.xml';
$f = fopen($xml, 'w') or die('cannot open file ' . $xml);
// try $n times to lock the file
// waiting 1 milli-second between each attempt
$n = 6;
while ($n) {
if (flock($f, LOCK_EX)) break;
--$n;
usleep(1000);
}
if ($n == 0) die('Could not lock the file' . $xml)
// write data
fwrite($f, 'whatever');
// unlock and close the file
flock($f, LOCK_UN);
fclose($f);
?>
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--