Re: Problem: Writing into Files?
| From: | Ollisso | Date: | Sun, 02 Aug 2009 09:44:03 +0000 |
| Subject: | Re: Problem: Writing into Files? | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-296178@lists.php.net to get a copy of this message | ||
On Sun, 02 Aug 2009 07:11:27 +0300, "Parham Doustdar" <parham90@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 = $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/