Forum Moderators: coopster

Message Too Old, No Replies

How to log to unique files?

         

may_hem1

10:44 pm on Sep 27, 2003 (gmt 0)

10+ Year Member



Hi, I've created a form on a Web page which allows a user to make comments and state any problems/issues.

At the moment, the forms logs the user issues to one long text file. I'd like it to log the issues to separate text files.

Any ideas how I can log each problem to separate text files and ensure that I don't overwrite any of the existing text files?

Here is the code so far, which logs to only 1 file so far. By the way, my Web space uses only PHP3.

ProcessForm.PHP:
-------------------
<?php

$myFileHandle = fopen('problem1.txt','a') or die("can't open file");

if (-1 == fwrite($myFileHandle,date("D j M Y g:i a").chr(10))) { die("can't write data"); }
if (-1 == fwrite($myFileHandle,$txtCompany.chr(10))) { die("can't write data"); }
if (-1 == fwrite($myFileHandle,$txtAddress.chr(10))) { die("can't write data"); }
if (-1 == fwrite($myFileHandle,$txtPhoneNumber.chr(10))) { die("can't write data"); }
if (-1 == fwrite($myFileHandle,$txtEmail.chr(10))) { die("can't write data"); }
if (-1 == fwrite($myFileHandle,$txtComments.chr(10))) { die("can't write data"); }
if (-1 == fwrite($myFileHandle,$txtProblem.chr(10))) { die("can't write data"); }
if (-1 == fwrite($myFileHandle,$txtProblemFound.chr(10))) { die("can't write data"); }
if (-1 == fwrite($myFileHandle,$txtReason.chr(10))) { die("can't write data"); }
if (-1 == fwrite($myFileHandle,$txtDateOfProblem.chr(10))) { die("can't write data"); }
if (-1 == fwrite($myFileHandle,$txtProblemCaused.chr(10))) { die("can't write data"); }
if (-1 == fwrite($myFileHandle,"-----".chr(10))) { die("can't write data"); }

fclose($myFileHandle) or die("can't close file");

?>
-------------------

Thanks for your suggestions,

May

jatar_k

10:56 pm on Sep 27, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



log the issues to separate text files

do you mean each individual issue to a seperate file or each type of issue to its own file?

also, you might want to build all of your data into one string and then just write to file once, it's simpler. ;)

may_hem1

11:15 pm on Sep 27, 2003 (gmt 0)

10+ Year Member



Hi Jatar_k,

I don't want to categorise or do anything else clever.

I just need to log every issue to a separate text file. So 30 problems makes 30 text files.

Thx for your help,
May

jatar_k

11:27 pm on Sep 27, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



generate the filename with a unix timestamp

$filename = time() . ".txt";
$fp = fopen($filename,'a');

$strtowrite = ""; //put all the values from the form together

fwrite($fp,$strtowrite);
fclose($fp);

may_hem1

11:49 pm on Sep 27, 2003 (gmt 0)

10+ Year Member



Cool! Thanks jatar_k!

I've used a slight variation - I've used the date and time this way:

$myFileHandle = fopen('problem-'.date("Ymd").'-'.date("His").'.txt','a') or die("can't open file");

This way, the files are named like this:
problem-20030927-164427.txt
which makes the date and time easy to read.

Now, if 2 people submit forms at the same moment in time could it cause a problem? The fact that we're opening the file with the 'a' attribute means it will append one problem to the other, right? This is ok, as we're not losing any data, but is there a way to force a new text file instead of appending?

Thx again, =o)
May

jatar_k

12:23 am on Sep 28, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



two people would have to submit at exactly the same second it would be very difficult and if for some strange reason it happened it would append the other info.

Odds are this setup will produce a new file for each request, except that very rare occasion. I'll bet that you don't have it happen. ;)