Emptying 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
User avatar
Zeceer
Forum Contributor
Posts: 136
Joined: Fri Aug 02, 2002 5:10 am
Location: Norway

Emptying file

Post 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.
Coco
Forum Contributor
Posts: 339
Joined: Sat Sep 07, 2002 5:28 am
Location: Leeds, UK
Contact:

Post 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
User avatar
Phirus
Forum Commoner
Posts: 37
Joined: Thu Apr 18, 2002 4:10 pm
Contact:

Post 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
User avatar
Zeceer
Forum Contributor
Posts: 136
Joined: Fri Aug 02, 2002 5:10 am
Location: Norway

Post 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
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

Phirus probably had the quickest way..

Code: Select all

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