Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Empty a file ... 1

Status
Not open for further replies.

dbeezzz

Technical User
Nov 30, 2005
54
KR
Is there any way to empty a file in php. Bascially I open a file and read in some values, then I feed the file into another function. I want to empty the file again for the next time.

There doesn't seem to be much functionality to fwrite() ? It doesn't give me the option of writing over the previous file contents.

Code:
$filename = './templates/Classic/pad.tpl';
 $fp = fopen($filename, "a");
 flock($fp, LOCK_EX);
 $string = $output;
 if ( !fputs($fp, $string) ) { die("There was an error writing to the file 1"); } 
 $template->set_filenames( array('fckeditor' => './templates/Classic/pad.tpl') );
 if ( !fwrite($fp, ' ') ) { die("There was an error writing to the file 2"); }
 
 flock($fp, LOCK_UN);
 fclose($fp);
Thanks
 
It all depends on what mode you use in fopen().

In the code above you are using 'a' which is used when you want to append the file contents. You will probably want to use 'w' or 'w+'. Both of these place the file pointer at the beginning of the file and truncate the file length to zero (erase the contents, in a sense). The 'w+' is used when you want to read and write to the file. 'w' is used when you only want to write to the file.
 
Thanks Itshim,
I should have noticed, I thought the fopen( a ) referred was a reference to 'all'.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top