17
<?php
// Define the file path for the log file
$logFile = 'path/to/your/logfile.txt';
// Message to be logged
$message = "Log this message.";
// Open the log file in append mode
if ($handle = fopen($logFile, 'a')) {
// Get the current date and time
$timestamp = strftime("%Y-%m-%d %H:%M:%S", time());
// Write the log message along with timestamp to the file
fwrite($handle, "[$timestamp] $message\n");
// Close the file handle
fclose($handle);
} else {
// Handle error if unable to open the log file
echo "Unable to open log file for writing.";
}
?>
This script will append a message along with a timestamp to the specified log file. Make sure to replace 'path/to/your/logfile.txt' with the actual path where you want to store the log file.
Remember to set appropriate permissions on the directory where the log file is stored so that PHP can write to it.