这段代码允许:1( 创建文件夹2( 创建备份文件3(消除数据库中存在的一些数据,相对于时间间隔。
在本地,这段代码运行良好,在线报告后,正确创建文件夹,创建文件,删除数据库中的值,但不会将已删除的文件写入备份文件,经过测试,我发现自己有一些已删除的数据,而不是备份。
这是一个问题,所以我的问题是,由于擦除发生了但没有写任何东西,它会是什么?
如何为要编写的 $ ok 变量进行虚构的测试?
<?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();
}
?>
这是一个文件夹权限问题。
允许在您的在线文件夹中写入
看看这个
如何在 Linux 中一步更改文件夹及其所有子文件夹和文件的权限?
您甚至可以使用 chmod(( 更改来自 PHP 的权限
// Read and write for owner, nothing for everybody else
chmod("/somedir/somefile", 0600);
// Read and write for owner, read for everybody else
chmod("/somedir/somefile", 0644);
// Everything for owner, read and execute for others
chmod("/somedir/somefile", 0755);
// Everything for owner, read and execute for owner's group
chmod("/somedir/somefile", 0750);
https://www.php.net/manual/fr/function.chmod.php