Re: Is This Code OK?
| From: | Teodor Cimpoesu | Date: | Sat, 21 Oct 2000 09:32:04 +0000 |
| Subject: | Re: Is This Code OK? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-21539@lists.php.net to get a copy of this message | ||
Hi iGuru!
On Sat, 21 Oct 2000, iGuru wrote:
> Can anyone please check if the below mentioned code is correct or can be
> reduced further. I am trying to make a simple page counter with this code.
> Note that the data.txt file only contains the number '1' at startup.
>
> Can you please check if the below mentioned code is correct or can be
> reduced further. I am trying to make a simple page counter with this code.
> Note that the data.txt file only contains the number '1' at startup.
>
> <?php
> $file_loc = "data.txt";
>
> $oldfile = fopen($file_loc,"r");
> $file_contents = fread($oldfile,filesize($file_loc));
> $oldval = $file_contents;
> fclose($oldfile);
>
> $newfile = fopen($file_loc, "w+");
> echo $old;
> $old = fread($newfile, $newfile);
> $newval=$oldval+1;
> fwrite($newfile, "$newval");
> fclose($newfile);
> echo $newval;
> ?>
>
>
here's my version of the above problem:
define ('CNT_LEN', 8);
define ('CNT_FILE', './index.db');
if (!is_readable (CNT_FILE)) return ;
$fd = fopen (CNT_FILE, 'r+');
if ($fd == false) return;
flock ($fd, LOCK_EX);
$idx = fread ($fd, 128);
settype ($idx, 'double');
++$idx;
rewind ($fd);
fwrite ($fd, $idx);
flock ($fd, LOCK_UN);
fclose ($fd);
$idx = sprintf ('%0'.CNT_LEN.'d',$idx);
$n = strlen ($idx);
?>
<table cellspacing=0 cellpadding=0 style='border: inset #D0D0D3'>
<tr>
<td>
<? for ($i = 0 ; $i < $n ; ++$i) {
print "<img src='images/$idx[$i].gif' border=0>";
}
?></td>
</tr>
</table>
you just
include' it. Note the locking' to avoid compromising the
file content.
-- teodor