Emptying a text 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
.Stealth
Forum Commoner
Posts: 57
Joined: Wed Jan 10, 2007 12:15 pm
Location: Manchester, England

Emptying a text file?

Post 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.
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

unlink() and fopen() (as Aaron pointed out)
.Stealth
Forum Commoner
Posts: 57
Joined: Wed Jan 10, 2007 12:15 pm
Location: Manchester, England

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
.Stealth
Forum Commoner
Posts: 57
Joined: Wed Jan 10, 2007 12:15 pm
Location: Manchester, England

Post 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 :)
Post Reply