create a "daily" text file and append data to it all day

Hi

I want to log certain things a user does on my site to a dedicated "daily" file, appending to it all day, then creating a new one if its a new date and so on.

Idea file name YYYYMMDD.txt

Could anyone assist with the full code to do this please?

Regards
Neil
LVL 3
Neil ThompsonSenior Systems DeveloperAsked:
Who is Participating?
 
Julian HansenConnect With a Mentor Commented:
$filename = date('Ymd') . '.txt';

Add to file with append option
example

file_put_contents($filename, $data, FILE_APPEND);

Where $data holds the text you want to write to the file - you can also use fwrite

This way filename will auto roll over to the next day
0
 
Ray PaseurCommented:
In case you want to do any "data mining" or look for trends in the data you might want to use a data base table instead.

Your table definition would consist of a DATETIME column and a column (or perhaps a few columns) to contain the "certain things."  If you can tell us what these certain things are it would probably be easy to show you how to create and use the table.

The advantage of this over flat files would be that reporting, searches and pattern discernment would not have to consider multiple files (and possibly missing files).
0
 
Neil ThompsonSenior Systems DeveloperAuthor Commented:
Superb, just what I needed regards
Neil
0
 
Neil ThompsonSenior Systems DeveloperAuthor Commented:
Many thanks Ray, Julians post did exactly what I needed for that particular item as I'm already implimenting a solution similar to your suggestion for other logging parts.

Regards
Neil
0
 
Julian HansenCommented:
You are welcome - thanks for the points.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.