Page 1 of 1

Emptying a CSV file and starting fresh daily

Posted: Wed Apr 12, 2006 3:04 pm
by squatchimo
I apologize if this has been asked before but I found no trace of this in the search.

I have a form that a customer fills out that emails data to the site owner and also appends itself to a CSV file on the server. Up to now, the CSV file emails itself to the owner daily. The problem is that the data is always appended to the CSV file and all of the old data is there each day.

Is it possible from PHP to "purge" the CSV file after it's been sent so that it contains only new data? I have limited access to the server so I can't delete the file and have it generate a new one. I need the current file to just empty itself and start over. Does that make sense?

Posted: Wed Apr 12, 2006 3:07 pm
by feyd
can you open the file for writing? If so, simply open it (without the append mode) and close it. It will be emptied.

Posted: Wed Apr 12, 2006 3:23 pm
by squatchimo
Thanks for the reply feyd. I tried utilizing your idea with the following code:

Code: Select all

$fp = fopen('file.csv');
fwrite($fp);
fclose($fp);
That snippet runs after the code for mailing the CSV as an attachment. I tested it and the file still contains all original data after each send. What am I missing? Is the code above supposed to do what you said?

EDIT:
I also tried:

Code: Select all

fopen('file.csv');
fwrite('file.csv');
fclose('file.csv');
Yes, I can write to the file fine. All data submitted goes into the CSV just fine. Any clues? Thanks!

Posted: Wed Apr 12, 2006 3:39 pm
by feyd

Code: Select all

$fp = fopen('file.csv', 'w');
fclose($fp);

Posted: Wed Apr 12, 2006 3:42 pm
by squatchimo
Brilliant! Thank you so much feyd!