Page 1 of 1

php txt file >>>>>>

Posted: Mon Mar 15, 2004 11:15 pm
by timothye
hey i have a chat program ,,flash and php i was wondering if its possible to clear the chat file periodically ?for an example if 1,000 lines were written can i delete the txt file 's contents ?
im a newbie in php and i dont know where to look ..thanks for any help !

cheers timo

Posted: Mon Mar 15, 2004 11:17 pm
by Goowe
Though I'm sure someone else here might have a better solution, you could go by the filesize of the file where the chat is stored.

http://us2.php.net/manual/en/function.filesize.php

if(filesize('path/to/file') > 5000)
{
unlink('path/to/file'); // Deletes file
}

But if you delete it, you have to create a clean one ;)

Posted: Tue Mar 16, 2004 12:48 am
by vigge89
instead, you could take away (delete) the last 1000 letters

Posted: Wed Mar 17, 2004 1:03 pm
by timothye
yes sorry for bumping this up but it would be great if someone had a simple snippet ..
i just want to know how to delete the lines in a txt file after a certain ammount of txt has been witten to the file .. unlink isnt working and delete hasnt worked either .. im a newb to php so please explain so i may learn from your replies .. thanks ..

Posted: Wed Mar 17, 2004 6:53 pm
by Goowe
What is your syntax for the unlink() function?

What I wrote above would delete the file after such and such in bytes.

http://us2.php.net/manual/en/function.filesize.php
http://us2.php.net/manual/en/function.unlink.php

Posted: Wed Mar 17, 2004 7:13 pm
by JAM
Try this and see if that gets you anywhere...

Code: Select all

<?php
    if (!file_exists('test.txt') or filesize('test.txt') < 200) {
        $f = fopen('test.txt', 'a');
        fwrite($f, "Line added at filesize: ". filesize('test.txt') . "\n");
        fclose($f);
    } else {
        $f = fopen('test.txt', 'w');
        fclose($f);
    }
    show_source('test.txt');
?>
I'm using the fopen with the 'w' mode to truncate the textfile.

Posted: Wed Mar 17, 2004 8:34 pm
by seeker2921
I'm woundering if you found this flash chat on Swish-Tutorials.com reason being I found a flash chat on there that use's a txt file and I needed it to be cleared from time to time as well..

I was thinking about changing it to use an MySQL db instead of the flat txt file.. But I have yet to mess with it..