Forum Moderators: coopster

Message Too Old, No Replies

clearing file after appended certain # of times

how to clear a text file after it has been appended a certain amount of tim

         

yt404

2:06 pm on May 1, 2004 (gmt 0)

10+ Year Member



ok i am new to php and have started writing my first program, i am writing a small shoutbox, however i have come accross two problems, the main problem is that the contents of the file will get bigger and bigger, until there is too much to display clearly, so i would like to know if it is possible to clear the file after it has been appended a certain amount of times, this will then only display 10 entries on the page that includes the include() of the file instead of 10000, can anyone help?

also this is quite a small problem i have used th following code:

<?php
$filename = "test.txt";
if ( $a = fopen( $filename, "a")) {
flock( $a, LOCK_SH );
fwrite($a, $message);
flock( $a, LOCK_UN );
} else {
print "file couldn't be opened";
}
fclose( $a );
?>

After $message in the fwrite() function i have tried using <br> tags enclosed in single and double quotes, i have tried using \n\n to make it so that the next entry will be on a new line, i get a parse error, can anyone help with this too?

it is just a first script but i would really like to know how to set a maximum amount of entries to display on the page, without having to store all of the entries in the file like only displaying 10 but having 10000 in the text file, i want to display 10 and only have 10 in the text file.

Netizen

3:20 pm on May 1, 2004 (gmt 0)

10+ Year Member



I think you need to read in the file, add the line you want, count how many lines you have and then remove any at the beginning if it is over 10. This is assuming each entry is on a single line.

<?php
$filename = "test.txt";

$contents=file($filename);

array_push($contents,$newEntry);

while (count($contents) > 10) {
array_shift($contents);
}

then do:

if ( $a = fopen( $filename, "a")) {
flock( $a, LOCK_SH );
fwrite($a, $contents);
flock( $a, LOCK_UN );
} else {
print "file couldn't be opened";
}
fclose( $a );

?>

I am not sure about explicity locking the files - I have never had to do this.

You should be able to put <br> in your entries like

$newEntry=preg_replace("/\n/","<br>",$newEntry)

which will

a) replace newlines with <br>
b) mean each entry is on one line which makes it easier to manage the entries

yt404

4:09 pm on May 1, 2004 (gmt 0)

10+ Year Member



cheers man, that seems to be workin, one thing, where in the code do i put the

$newEntry=preg_replace("/\n/","<br>",$newEntry)

this might also have something to do with the fact that instead of saving the contents of the form field into the file it is saving the word Array, i have only been doing it a week :s

yt404

6:13 pm on May 1, 2004 (gmt 0)

10+ Year Member



i believe the problem is due to the fact that not every entry is put onto a new line in the file, can anyone point me in the direction of how to do this?

Elijah

6:33 pm on May 1, 2004 (gmt 0)

10+ Year Member



Try changing this line:
fwrite($a, $message);

to:
fwrite($a, "\n".$message);

That should make each message be added on a new line.

yt404

8:43 pm on May 1, 2004 (gmt 0)

10+ Year Member



got it sussed m8, cheers for the help, started to understand it all now, I am looking into using an if statement to say

if filesize($filename) > 512 { delete contents of file }

got to look into it further, can anyone tell me though if 512 would be accepted as 512 kilobits or 512 bytes?

cheers for your help so far peeps