Re: fclose() problem
| From: | David Robley | Date: | Fri, 07 Jul 2000 06:47:35 +0000 |
| Subject: | Re: fclose() problem | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-5330@lists.php.net to get a copy of this message | ||
On 7 Jul, Peter Lewis wrote:
> Please excuse my writing to this list if it the wrong one.
> Let me know and I'll redirect
>
>
> Hello all,
>
> I have a major problem with my PHP3 script!
>
> a) My count file is "pcnum.txt" - permissions 0666 rw-rw-rw-
> b) It contains the value of 1855
> c) I wish to open this file, increment the count, rewrite the value.
> d) Using Redhat Linux 6.0 - Apache 1.3.13 with phpversion() of 3.0.7 php_mod
> e) my script is:- //line numbers are NOT in script
> <?
> 1 $name="pcnum.txt";
> 2 $file=fopen($name,"w");
Oh dear - that's just destroyed the contents of the file. Make that
$file = fopen(($name,"r");
> 3 $Count=fread($file(filesize($name));
The arguments to fread need to be separated by a comma;
$Count=fread($file, filesize($name));
> 4 fclose($file);
$Count is a string value, so need to cast it as integer to do
incrementing
$Count = intval($Count);
> 5 $Count++; // also used $Count += 1
> 6 $fd=fopen($name,"w");
> 7 fputs($fd,$Count);
> 8 fclose($fd);
> ?>
> The only problem is, It doesn't work :-(
>
So we get
<?php
$name="pcnum.txt";
$file=fopen($name,"r");
$Count=fread($file, filesize($name));
$close = fclose($file);
$Count = intval($Count);
$Count++; // also used $Count += 1
$fd=fopen($name,"w");
fputs($fd,$Count);
fclose($fd);
?>
which works a charm in incrementing the counter but of course, run as
is, will return Document contains no text.
As a further exercise, consider the case where there are multiple
accesses to the counter script almost simultaneously >:-)
Cheers
--
David Robley | WEBMASTER & Mail List Admin
RESEARCH CENTRE FOR INJURY STUDIES | http://www.nisu.flinders.edu.au/
AusEinet | http://auseinet.flinders.edu.au/
Flinders University, ADELAIDE, SOUTH AUSTRALIA