Skip to content
Advertisement

PHP, Date() and fopen

I have an error system that take the date() and the error and insert it into a file:

$fp = fopen('errorFile.txt', 'a');
$message = "At the time: " . date("Y,m,d|H:i:s") . " the following error took place: " . $e->getMessage();
fwrite($fp, $message);
fclose($fp);

My problem: for start at the fopen at the second parameter I need the pointer to be at the start and that looked at the manual and I need the parameter to be writing only, put the pointer at the start and not truncate the file to zero length, and the only I found is the parameter ‘a’ all is good with ‘a’ except the pointer at the end each time, so if anyone know what parameter can I use, so the pointer at the start and it’s write only and it wont truncate the file to zero length, also I am trying as you see to insert the date and for example: the time here is 18:00 the time was inserted is 15:00.

Advertisement

Answer

You can try this logic

$message = "At the time: " . date("Y,m,d|H:i:s") . " the following error took place: " . $e->getMessage();

$file = '/path/to/file';

$fileContents = file_get_contents($file);

file_put_contents($file, $message . $fileContents);
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement