Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 246
  • Last Modified:

How can I add data to an existing log file?

Hi Guys,
The following code does a good job of placing the $stringData into the $log_file.
But it replaces any existing data that's already in the $log_file.

How should I write this so that it will add the $stringData to the existing content instead of replacing it?

Thanks in advance for the help,
Sas
<?php
$email=$_REQUEST['Visitors_eMail'];
$Today = date("F m, Y",time());

$log_file = "../directory_1/VisitorLog";
$fh = fopen($log_file, 'w') or die("can't open file");

$stringData = "
Date: $Today
E-mail: $email \n";

fwrite($fh, $stringData);
fclose($fh);
?>

Open in new window

0
sasnaktiv
Asked:
sasnaktiv
2 Solutions
 
DavidCommented:
the 'w' in the fopen means create a new file.  You want to use 'a' to append
0
 
Robert SaylorSenior DeveloperCommented:
The 'a' should do it as mentioned above.

Here are some more options depending on how you want to handle the file.

Note: This is from php's site.

http://php.net/manual/en/function.fopen.php
0
 
sasnaktivAuthor Commented:
Thanks Guys,
That was very helpful, and as I suspected a simple solution.
The extra info from rsaylor should be helpful too.
Enjoy your day,
Sas
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now