php txt file >>>>>>

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
timothye
Forum Newbie
Posts: 15
Joined: Sun Feb 01, 2004 2:36 pm
Location: Sweden/Sverige/USA

php txt file >>>>>>

Post 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
Goowe
Forum Commoner
Posts: 94
Joined: Mon Mar 15, 2004 9:51 am
Location: Southeast Alaska

Post 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 ;)
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

instead, you could take away (delete) the last 1000 letters
User avatar
timothye
Forum Newbie
Posts: 15
Joined: Sun Feb 01, 2004 2:36 pm
Location: Sweden/Sverige/USA

Post 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 ..
Goowe
Forum Commoner
Posts: 94
Joined: Mon Mar 15, 2004 9:51 am
Location: Southeast Alaska

Post 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
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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.
seeker2921
Forum Contributor
Posts: 120
Joined: Sat Mar 22, 2003 7:10 pm
Location: Wiesbaden Germany
Contact:

Post 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..
Post Reply