deko wrote:[color=blue]
> Is there a way to avoid opening and closing the file "viscount" twice in this
> code:[/color]
Yes
<?php
// open file for reading and writing
if (!$fh = @fopen('viscount', 'r+')) exit('fopen failed');
// lock it, try ten times before giving up
$lock_tries = 10;
while (!flock($fh, LOCK_EX)) {
usleep(100);
--$lock_tries;
if (!$lock_tries) break;
}
if (!$lock_tries) exit('flock (to lock) failed');
// read data
if (!$line = @fgets($fh)) exit('fgets failed');
// do whatever you want with the data (... but do it fast)
$line = (int)(trim($line));
++$line;
// go to beginning of file
if (@fseek($fh, 0)) exit('fseek failed');
// clear all data in file (maybe not needed ???)
if (!ftruncate($fh, 0)) exit('ftruncate failed');
// write new data
if (!fwrite($fh, $line . "\n")) exit('fwrite failed');
// unlock file, try ten times before giving up
$lock_tries = 10;
while (!flock($fh, LOCK_UN)) {
usleep(100);
--$lock_tries;
if (!$lock_tries) break;
}
if (!$lock_tries) exit('flock (to unlock) failed');
// close file
if (!fclose($fh)) exit('fclose failed');
// file updated, let the user know about it
exit('File updated, thank you for using our service.');
?>
--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :