Faqts : Computers : Programming : Languages : PHP : Common Problems : Files : Tips and Tricks : File Handling

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

1 of 3 people (33%) answered Yes
Recently 1 of 3 people (33%) answered Yes


Is there any way I can empty a text file? Or rather just remove the first line?

Apr 28th, 2004 16:01
Mike Chavez, Umang Beri,

You can destroy the entries in a file using the "write" attribute:
#Opens the file for writing, but also destroys the contents of a 
#file. Note that this also places the file pointer at the beginning of 
#the file.
     $fh = fopen($myfile, "w");
You can also remove the first line by writing to a file in r+ mode or 
rewinding the file to the beginning, then writing a blank line:
     $fh = fopen($myfile, "r+");
     fwrite($fh, "\n");
     fwrite($fh, "\n");
I'm not quite sure this will work correctly (as I don't have access to 
a php server right now and I'm doing it from the top of my head), but 
the idea is that we want to write a blank line to the file and r+ and 
rewind already places the pointer at the beginning of the file. Hope 
that helps some...