Page 1 of 1

Emptying a text file?

Posted: Thu Jan 11, 2007 11:30 pm
by .Stealth
hello again lol.

i have a simple news script, and its just perfect, ive made it for somebody who dosent know much and they have their own form to add news, the news goes into a .txt file and its exported to a certain part of his page.

what i want to do is add a link or button to empty that file, so that when his little news section gets full, he just hits delete and it emptys that file ready for more news.

is there a function or something for this? i had a look on php.net but nothing i can see there.

thanks.

Posted: Thu Jan 11, 2007 11:43 pm
by aaronhall
I believe this should work:

Code: Select all

<?php
$filepath = '/path/to/file.txt';
$fres = fopen($filepath, 'w');
fclose($fres);
?>
Opening a file with the 'w' option will truncate the file to zero length and place the pointer at the beginning of the file.

Posted: Thu Jan 11, 2007 11:43 pm
by feyd
unlink() and fopen() (as Aaron pointed out)

Posted: Thu Jan 11, 2007 11:52 pm
by .Stealth
thanks for that, just another question though, how do i execute that lol, sorry, im super new at this :oops: , im slowly learning though :)

would i wrap it in my own function? then use something to execute the function?


i want it so he just clicks something like a "empty news" link and it magically emptys the file :)

thanks

Posted: Fri Jan 12, 2007 12:04 am
by feyd
A separate script or a URL argument are both fairly common for such an action.

A URL argument is more complicated due to the incorporation into an existing script.

Posted: Fri Jan 12, 2007 2:27 am
by .Stealth
thanks for that, stupid question really lol.

i did it though, just put it in a simple page with a 4 second meta re-direct and it works like a dream, thanks for your help :)