[PHP] Re: Problem: Writing into Files?

2009-08-03 Thread Ollisso
On Sun, 02 Aug 2009 13:14:42 +0300, Parham Doustdar parha...@gmail.com 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 =

Re: [PHP] Re: Problem: Writing into Files?

2009-08-03 Thread Richard Heyes
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

Re: [PHP] Re: Problem: Writing into Files?

2009-08-02 Thread Richard Heyes
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

[PHP] Re: Problem: Writing into Files?

2009-08-02 Thread Ollisso
On Sun, 02 Aug 2009 07:11:27 +0300, Parham Doustdar parha...@gmail.com 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

[PHP] Re: Problem: Writing into Files?

2009-08-02 Thread Parham Doustdar
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);

Re: [PHP] Re: Problem: Writing into Files?

2009-08-02 Thread Parham Doustdar
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:

Re: [PHP] Re: Problem: Writing into Files?

2009-08-02 Thread Richard Heyes
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: ?php fwrite(fopen('/tmp/counter', 'a'), '1'); ? The 1 could be any single byte character I guess. --

[PHP] Re: Problem: Writing into Files?

2009-08-01 Thread Ollisso
On Sat, 01 Aug 2009 08:20:23 +0300, Parham Doustdar parha...@gmail.com 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 .

[PHP] Re: Problem: Writing into Files?

2009-08-01 Thread Parham Doustdar
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);