Page 1 of 1

Emptying file

Posted: Mon Oct 14, 2002 9:26 am
by Zeceer
Is there any easy function for emptying files? If i have a file with some information, and want to erase all the info in it without unlink()'ing the file.? I can always read the name of the file, delete it and touch() a new one out of the info i got in a variable, but i'll rather not if theres any easier way.

Posted: Mon Oct 14, 2002 9:30 am
by Coco
well fopen can be used to truncate a file to 0 length... dunno how much that helps ya

http://www.php.net/manual/en/function.fopen.php

Posted: Mon Oct 14, 2002 9:36 am
by Phirus
Dont understand you 100% but this may help, If you are working with simple text files, It doesnt some much as empty, but in fact overwrites.

Code: Select all

<?php

fopen("somefile.txt", "w");

?>
Hope it helps,

Phirus

Posted: Mon Oct 14, 2002 9:53 am
by Zeceer
Found this: http://www.php.net/manual/en/function.ftruncate.php

I'll just read the lenght of the file and truncate the length :D

Posted: Mon Oct 14, 2002 4:36 pm
by hob_goblin
Phirus probably had the quickest way..

Code: Select all

<?
$fp = fopen("somefile.txt", "w");
fclose($fp);
?>
will just truncate it instantly.