I have a .txt file split into lines, and the items for each line are split into arrays log_dates and log_times. I'm trying to insert each log date and it's matching log time into my database which has log_date and log_time columns.
Access.txt
2014-03-16 13:57:35.089
2014-03-16 13:57:35.089
2014-03-16 13:57:35.089
2014-03-16 13:57:35.089
LogsModel.php
function __construct($db) {
try {
$this->db = $db;
} catch (PDOException $e) {
exit('Database connection could not be established.');
}
}
public function insertFileContents()
{
$filename = 'C:/Program Files/FileMaker/FileMaker Server/Logs/Access.log';
// Open the file
$fp = @fopen($filename, 'r');
// Add each line to an array
if ($fp) {
$lines = explode("
", fread($fp, filesize($filename)));
foreach($lines as $line){
$l = explode(" ", $line);
$log_dates[] = $l[0];
$log_times[] = $l[1];
$sql = ("INSERT INTO log_table VALUES (log_date, log_time) VALUES ('$log_dates', '$log_times')");
$query = $this->db->prepare($sql);
$query->execute();
}
return true;
}
}
Eventually I would like to have it so every time the page is accessed the database gets updated by the Access.txt file which will continue to accumulate new rows.