> I have a chat script that I downloaded and according to the license I
> can modify it as much as I want, and I'm a little stuck on a simple
> modification I want to make. When you type in a message it stores in
> a file called text.php and then the chat page does an include call for
> the text.php file.  Currently, the writer of this script has it setup
> all new text added is placed at the begining of the file using the
> command, according to php.net with fopen if I switch the "w+" to "a+"
> shoudl place the new data at the end of the file. However, when I make
> this switch,  the data is still being placed at the begining of the
> text.php file. I've including the portion of the script that writes to
> text.php can someone help me find what I'm not seeing?
> $filename = "text.php";
>                 $fileAr= file($filename);

It's reading the whole file into an array here. The newest lines will
still be first.

>                 exec("cat /dev/null > '$filename'");
>                 $fd = fopen( $filename, "w+" );

This opens the file and truncates it to zero length.

>                 $filemessage = "<a
> me</B></a>:
> ";
>                 $filemessage .="<font
> color=\"$fcolor\">$chat</font><BR>\n";
>                 fputs($fd,$filemessage);

This write the new, formatted message.
>                 $numLines = 20;
>                 for ($i=0;$i<$numLines;$i++) {
>                     fputs($fd,$fileAr[$i]);

And then write the first 20 lines of the file.

>                 }
>                 fclose( $fd );

So, if you want it in the new format of newest chat last in the file,
then use the same script, but fputs() the 20 old lines first, then the
new line. Just change the order.

This would work for you:

$numLines = 20;
$filename = "text.php";
//read whole file
$fileArr = file($filename);
//open file and truncate to zero
$fp = fopen($filename,"w+");
//write 20 lines of old file to new file
//format $message
//write message to file
//close file

Adapt to your needs...

---John Holmes...

PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to