[PHP] writing while reading

2001-08-01 Thread Rui Barreiros


hi all,

i'm writing a web chat, the frame that presents the text, has a very fast 
refresh (has to) it's about from 2 in 2 seconds, my problem is writing to the 
file (chat is file based), when the refresh read the file, it doesn't let php 
write. i made comething like this.

Function chat2file($filename, $str) {
  $fp = fopen(chats/$filename, a);
  fputs($fp, $str);
  close($fp);
}

Function file2chat($filename) {
  if(file_exists(chats/$filename)) {
$fd = fopen(chats/$filename, r);
flock($fd, LOCK_UN);
$content = fread($fd, filesize(chats/$filename));
fclose($fd);
return $content;
  } else {
return ;
  }
}

i put the non blocking option in flock while reading but still blocks and 
doesn't write, my question is it possible to write to a file while reading?!, 
if so how?

thx.

-- 
Rui Barreiros
   Software Developer

WEBSOLUT - Soluções Internet
Emailto: [EMAIL PROTECTED] 
Personal Info: http://websolut.net/people/rui.html

As informações contidas neste email são confidenciais
e destinam-se apenas à(s) pessoa(s) a quem foi enviado:
http://websolut.net/confidencialidade-responsabilidade.html

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




RE: [PHP] writing while reading

2001-08-01 Thread scott [gts]

try only locking the file on a write, *not* on a read.
you are going to have many times more reads than writes,
even on the busiest day 

what you need to have is a blocking-lock your writes
and not bother with locks for reading.

the way you have it setup now, there's no locking
for writes, so you could definately have two instances
of PHP trying to write to the same file simultaneously
and corrupt your data.

besides, if you lock only for writing, and not for
reading, you can read the file while it's being written -
but you can never write twice.

 -Original Message-
 From: Rui Barreiros [mailto:[EMAIL PROTECTED]]
 Subject: [PHP] writing while reading
   hi all,
 
   i'm writing a web chat, the frame that presents the text, has a very fast 
 refresh (has to) it's about from 2 in 2 seconds, my problem is writing to the 
 file (chat is file based), when the refresh read the file, it doesn't let php 
 write. i made comething like this.
 
 Function chat2file($filename, $str) {
   $fp = fopen(chats/$filename, a);
   fputs($fp, $str);
   close($fp);
 }
 
 Function file2chat($filename) {
   if(file_exists(chats/$filename)) {
 $fd = fopen(chats/$filename, r);
 flock($fd, LOCK_UN);
 $content = fread($fd, filesize(chats/$filename));
 fclose($fd);
 return $content;
   } else {
 return ;
   }
 }
 
 i put the non blocking option in flock while reading but still blocks and 
 doesn't write, my question is it possible to write to a file while reading?!, 
 if so how?
 
 thx.


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]