Emptying a CSV file and starting fresh daily

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
squatchimo
Forum Commoner
Posts: 28
Joined: Thu Feb 03, 2005 3:36 pm

Emptying a CSV file and starting fresh daily

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
squatchimo
Forum Commoner
Posts: 28
Joined: Thu Feb 03, 2005 3:36 pm

Post 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!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$fp = fopen('file.csv', 'w');
fclose($fp);
squatchimo
Forum Commoner
Posts: 28
Joined: Thu Feb 03, 2005 3:36 pm

Post by squatchimo »

Brilliant! Thank you so much feyd!
Post Reply