July 10th, 2000, 04:45 AM
-
Hi again, My script= <? $fd=fopen("MyTest.txt","r+"); //read/write
flock($fd,2); $MyCount=fread($fd,filesize("MyTest.txt"));
$MyCount=intval($MyCount); $MyCount=$MyCount+100;
fputs($fd,$MyCount); fclose($fd); ?>
This opens the file reads the value and rewrites the new value BUT I notice that it
-appends- ie orig num = 111211 + 100 = 111311 but in file I have
111211111311.
Do I need to close then reopen the file or is there another command to use?
Thanks heaps, **** Title misleading - shd be (maybe) how to stop appending> ****
------------------
With my kind regards,
Peter.
[This message has been edited by pjlewis (edited July 10, 2000).]
July 11th, 2000, 02:41 AM
-
Hi,
just try this in a different way.
Read the value from the file first.then again open it in write mode and just write the new value to it..
try something like following:
<?
$fd=fopen("MyTest.txt","r");
$MyCount=fread($fd,filesize("MyTest.txt"));
//$MyCount=intval($MyCount);
echo $MyCount;
fclose($fd);
$fh=fopen("MyTest.txt","w");
$count=$MyCount+100;
fputs($fh,$count);
fclose($fh);
?>
i have not tested this ..Any way give a try..
Good Luck!!!
------------------
SR -
webshiju.com
"The fear of the LORD is the beginning of knowledge..."
July 11th, 2000, 02:52 AM
-
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by Shiju Rajan:
Hi,
just try this in a different way.
Read the value from the file first.then again open it in write mode and just write the new value to it..
try something like following:
<?
$fd=fopen("MyTest.txt","r");
$MyCount=fread($fd,filesize("MyTest.txt"));
//$MyCount=intval($MyCount);
echo $MyCount;
fclose($fd);
$fh=fopen("MyTest.txt","w");
$count=$MyCount+100;
fputs($fh,$count);
fclose($fh);
?>
i have not tested this ..Any way give a try..
Good Luck!!!
Thats fine, however I wish to lock the file so no one else can access between read/write. Just in Case!!
[/quote]
------------------
With my kind regards,
Peter.
July 11th, 2000, 03:19 AM
-
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by Shiju Rajan:
ok..
use flock() function before writing to the file..
$fh=fopen("MyTest.txt","w");
flock($fh,2);
//exclusive lock --writer.
$count=$MyCount+100;
fputs($fh,$count);
fclose($fh);
-------
[/quote]Ok. Am I being too paranoid? eg. You enter my site get count of 100 - Almost immediatly someone else enters and because I haven't locked it will also get a count of 100. When these are written to the file the result will be 200 for you and 200 for other uses. I want 200 for you and 300 for other.
------------------
With my kind regards,
Peter.
July 11th, 2000, 04:25 AM
-
sorry,
A small change in my previous post...
you should remove the lock beofore writing in to the file.after writing just put the exclusive lock..
i am not sure the locking and releazing numbers.just chck it out the manual...
$fh=fopen("MyTest.txt","w");
flock($fh,3);
//release a lock ...
$count=$MyCount+100;
fputs($fh,$count);
flock($fh,2);
//exclusive lock --writer.
fclose($fh);
------------------
SR -
webshiju.com
"The fear of the LORD is the beginning of knowledge..."