Re: [PHP] Re: Problem: Writing into Files?
Hi > The thing with this method is that it's just like the previous one; it opens > then adds something. I'm assuming if again two visitors visit at the same > time, this'd reset to zero. No, the "a" mode (IIRC) handles file locking for you. Even if it doesn't, the file won't be truncated, so you won't lose your count. -- Richard Heyes HTML5 graphing: RGraph - www.rgraph.net (updated 25th July) Lots of PHP and Javascript code - http://www.phpguru.org -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Problem: Writing into Files?
On Sun, 02 Aug 2009 13:14:42 +0300, "Parham Doustdar" wrote: Now this one always sets the file to one for some reason. I'll reiterate just in case there's been a misunderstanding on my part: [code] $fp = fopen($f, "r+"); while (!flock($fp, LOCK_EX)) sleep(1); $count = fgets($fp,filesize($fp)); ftruncate($fp, 0); fwrite($fp, ($count+1)); flock($fp, LOCK_UN); fclose($fp); Thanks a lot again for your help. This will work: $f = 'file.txt'; $fp = fopen($f, "r+"); while (!flock($fp, LOCK_EX)) sleep(1); $count = intval(fread($fp,1024)); fseek($fp,0,SEEK_SET); ftruncate($fp, 0); fwrite($fp, ($count+1)); fclose($fp); echo $count; (first you should create file file.txt manually, only then it will work) -- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Problem: Writing into Files?
> I don't quite know how I can write a bite into a file. I also looked into a > manual and couldn't find a mention of FLock-ing in the explaination for > FOpen parameters. Ok, from memory: The 1 could be any single byte character I guess. -- Richard Heyes HTML5 graphing: RGraph - www.rgraph.net (updated 25th July) Lots of PHP and Javascript code - http://www.phpguru.org -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Problem: Writing into Files?
Dear Richard, I don't quite know how I can write a bite into a file. I also looked into a manual and couldn't find a mention of FLock-ing in the explaination for FOpen parameters. Thanks a lot for your help. -- --- Contact info: Skype: parham-d MSN: fire_lizard16 at hotmail dot com email: parham90 at GMail dot com "Richard Heyes" wrote in message news:af8726440908020221r22e1efb3g321ba4140bfa4...@mail.gmail.com... > Hi, > >> ... > > You can write a single byte to the file to "increment" the counter, > then to read the count just use filesize(). I believe the a fopen() > mode will handle locking for you. It will result in a slowly growing > file, but space isn't exactly at a premium nowadays, and when the file > gets to a certain size (eg 1 gazillion k) you could use a summary > file. > > -- > Richard Heyes > HTML5 graphing: RGraph - www.rgraph.net (updated 25th July) > Lots of PHP and Javascript code - http://www.phpguru.org -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Problem: Writing into Files?
Now this one always sets the file to one for some reason. I'll reiterate just in case there's been a misunderstanding on my part: [code] $fp = fopen($f, "r+"); while (!flock($fp, LOCK_EX)) sleep(1); $count = fgets($fp,filesize($fp)); ftruncate($fp, 0); fwrite($fp, ($count+1)); flock($fp, LOCK_UN); fclose($fp); Thanks a lot again for your help. -- --- Contact info: Skype: parham-d MSN: fire_lizard16 at hotmail dot com email: parham90 at GMail dot com "Ollisso" wrote in message news:op.ux03zprl48v...@ol-n.kyla.fi... > On Sun, 02 Aug 2009 07:11:27 +0300, "Parham Doustdar" > wrote: > >> Dear Ollisso, >> I tried it with FLock() but it still didn't work. This is what I did: >> [code] >> $fp = fopen($f, "r"); >> $count =fgets($fp, 1024); >> fclose($fp); >> $fw = fopen($f, "w"); >> while (!flock($fw, LOCK_EX)) >> sleep(1); >> $cnew = $count + 1; >> $countnew = fputs($fw, $count + 1); >> flock($fw, LOCK_UN); >> fclose($fw); >> [/code] >> >> Am I doing anything wrong here? >> Thanks! > Hello, > > Actually you should do something like this (according to manual) > > $f=fopen("file", "r+"); > while(!flock($f, LOCK_EX)) sleep(1); > $count = fgets($f,1024); > ftruncate($f, 0); > fwrite($f, $count+1); > fclose($f); > > Problem in your case: first time you open file for reading, but you don't > flock it, so it can read non-writen file. > > > > > -- > Using Opera's revolutionary e-mail client: http://www.opera.com/mail/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Problem: Writing into Files?
On Sun, 02 Aug 2009 07:11:27 +0300, "Parham Doustdar" wrote: Dear Ollisso, I tried it with FLock() but it still didn't work. This is what I did: [code] $fp = fopen($f, "r"); $count =fgets($fp, 1024); fclose($fp); $fw = fopen($f, "w"); while (!flock($fw, LOCK_EX)) sleep(1); $cnew = $count + 1; $countnew = fputs($fw, $count + 1); flock($fw, LOCK_UN); fclose($fw); [/code] Am I doing anything wrong here? Thanks! Hello, Actually you should do something like this (according to manual) $f=fopen("file", "r+"); while(!flock($f, LOCK_EX)) sleep(1); $count = fgets($f,1024); ftruncate($f, 0); fwrite($f, $count+1); fclose($f); Problem in your case: first time you open file for reading, but you don't flock it, so it can read non-writen file. -- Using Opera's revolutionary e-mail client: http://www.opera.com/mail/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Problem: Writing into Files?
Hi, > ... You can write a single byte to the file to "increment" the counter, then to read the count just use filesize(). I believe the a fopen() mode will handle locking for you. It will result in a slowly growing file, but space isn't exactly at a premium nowadays, and when the file gets to a certain size (eg 1 gazillion k) you could use a summary file. -- Richard Heyes HTML5 graphing: RGraph - www.rgraph.net (updated 25th July) Lots of PHP and Javascript code - http://www.phpguru.org -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Problem: Writing into Files?
Dear Ollisso, I tried it with FLock() but it still didn't work. This is what I did: [code] $fp = fopen($f, "r"); $count =fgets($fp, 1024); fclose($fp); $fw = fopen($f, "w"); while (!flock($fw, LOCK_EX)) sleep(1); $cnew = $count + 1; $countnew = fputs($fw, $count + 1); flock($fw, LOCK_UN); fclose($fw); [/code] Am I doing anything wrong here? Thanks! -- --- Contact info: Skype: parham-d MSN: fire_lizard16 at hotmail dot com email: parham90 at GMail dot com "Ollisso" wrote in message news:op.uxy29woc48v...@ol-n.kyla.fi... > On Sat, 01 Aug 2009 08:20:23 +0300, "Parham Doustdar" > wrote: > >> Hi there, >> I've written a counter for my blog, which keeps the count of visitors in >> a file. However, when the visitors get too many, it resets to zero. Why? >> >> Here's the piece of code: >> >> [code] >> $f = $dir . '/view_counter' .EXT; >> $fp = fopen($f, "r"); >> $count =fgets($fp, 1024); >> fclose($fp); >> $fw = fopen($f, "w"); >> $cnew = $count + 1; >> $countnew = fputs($fw, $count + 1); >> return $cnew; >> [/code] >> >> I'm thinking this is caused by two visitors visiting the page at the >> same time; but is there a way to fix it, perhaps the reading/writing >> parameter? >> >> Thanks! >> > > Check: > http://www.php.net/flock > > > -- > Using Opera's revolutionary e-mail client: http://www.opera.com/mail/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Problem: Writing into Files?
On Sat, 01 Aug 2009 08:20:23 +0300, "Parham Doustdar" wrote: Hi there, I've written a counter for my blog, which keeps the count of visitors in a file. However, when the visitors get too many, it resets to zero. Why? Here's the piece of code: [code] $f = $dir . '/view_counter' .EXT; $fp = fopen($f, "r"); $count =fgets($fp, 1024); fclose($fp); $fw = fopen($f, "w"); $cnew = $count + 1; $countnew = fputs($fw, $count + 1); return $cnew; [/code] I'm thinking this is caused by two visitors visiting the page at the same time; but is there a way to fix it, perhaps the reading/writing parameter? Thanks! Check: http://www.php.net/flock -- Using Opera's revolutionary e-mail client: http://www.opera.com/mail/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php