This piece of code allows: 1) creation of a folder 2) creation of a backup file 3) elimination of some data present in the db, with respect to a time interval.
In local, this code, works perfectly, having reported it online, instead, creates the folder correctly, creates the file, deletes the values in the db, but does not write the deleted files in the backup file, having tested it, I found myself with some deleted data, not backup.
And this is a problem, so my question is what can it be since the erasure happens but nothing is written?
How do I make a test, fictitious for the $ ok variable to be written?
<?php
$databaseHost = 'xxx';
$databaseName = 'xxx';
$databaseUsername = 'xxx';
$databasePassword = 'xxx';
$connessione = mysqli_connect($databaseHost, $databaseUsername, $databasePassword, $databaseName);
$query = $connessione->query("
SELECT *
FROM Log_sds
WHERE data_log < DATE_SUB(NOW(), INTERVAL 7 DAY)
");
$results = $query->fetch_all(MYSQLI_ASSOC);
$ok = json_encode($results);
function makeDirectory($path, $mode)
{
if (!is_dir($path)) {
return mkdir($path, $mode, true);
} else {
echo $path . " already exist!";
}
}
$path = 'backup_LOG';
$mode = 0777;
// or you can add here that if exist does not call the function makeDirectory
if (!is_dir($path)) {
$risultato = makeDirectory($path, $mode);
}
$fileName = 'backup_LOG/backup_file_' . date('Y_m_d') . '.txt';
$file = fopen($fileName, 'a');
//$file = fopen($fileName, 'x+');
fwrite($file, $ok);
fclose($file);
if (count($results) > 0) {
$firstId = reset($results)['data_log'];
$lastId = end($results)['data_log'];
//$stmt = $connessione->prepare("DELETE FROM Log_sds WHERE data_log < DATE_SUB(NOW() , INTERVAL 7 DAY)");
$stmt->bind_param('ii', $firstId, $lastId);
$stmt->execute();
}
?>