Re: Failed to write to a text file (text file is RW) [message #171481 is a reply to message #171475] |
Wed, 05 January 2011 13:00 |
Denis McMahon
Messages: 634 Registered: September 2010
Karma:
|
Senior Member |
|
|
On 05/01/11 03:19, justaguy wrote:
> "
> You can single thread access on some OS's - see flock() in the PHP
> documentation.
> "
> sample code? Since I'm not a php coder and don't intend to become
> one, at least not now... could you save me some time?
This isn't perfect, but should allow you to output votes to a file that
you can tally later. If the file can't be seized for append, it backs
off up to 10 times for a random interval of 0.1 +- 0.05 secs and tries
again. It may be more robust than your approach. Doubtless others here
will pick it apart, but they prefer to do that rather than offer solutions.
<?php
// Use one of these file names as appropriate to your setup
$file = "/home/justaguy/path/poll_result.log";
$file = "data/poll_result.log";
$file = "poll_result.log";
$v = "no";
if ($vote == 1) $v = "yes";
$info = $_SERVER['REMOTE_ADDR'] . " " . date(DATE_RFC2822);
$vstr = $v . " " . $info . "\n";
$tries = 10;
while (!$fh = @fopen($file,"a") && $tries--) usleep(rand(50000,150000));
if ($fh) {
fwrite($fh,$vstr);
fclose($fh);
}
else {
error_log($info . ' open poll_result.log for append failed.');
}
?>
If after 10 attempts it still fails, it puts a message in wherever the
error log is defined as.
Code to open, read and tally the votes is left as an exercise for the OP.
Note, however, that this won't solve your original problem, which seems
to be that the process that is executing the php doesn't have write
access to the directory that the file is being created in.
Rgds
Denis McMahon
|
|
|