Re: Problem: Writing into Files?
| From: | Parham Doustdar | Date: | Sun, 02 Aug 2009 10:14:42 +0000 |
| Subject: | Re: Problem: Writing into Files? | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-296179@lists.php.net to get a copy of this message | ||
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" <ollisso@fromru.com> wrote in message
news:op.ux03zprl48vuyn@ol-n.kyla.fi...
> 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/