$cfile = "test.txt";
$fh = @fopen($cfile, "r+") or die("
Failed to open file $cfile.");
@flock($fh, LOCK_EX) or die("
Could not lock file $cfile.");
$s = @fgets($fh, 6);
$count = (int) $s + 1;
$count = str_pad($count, 6);
@rewind($fh) or die("
Failed to rewind file $cfile.");
if (@fwrite($fh, $count) == -1) {
die("
Failed to write to file $cfile.");
}
echo "$count";
@flock($fh, LOCK_UN) or die("
Could not unlock file $cfile.");
fclose($fh) or die("
Failed to close file $cfile.");
?>