Local does not match with online - PHP

时间:2019-04-08 12:53:16

标签: php

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();
}
?>

1 个答案:

答案 0 :(得分:0)

It's a folder permission problem.

Allow writing in your online folder

Take a look at this

How do I change permissions for a folder and all of its subfolders and files in one step in Linux?

You can even change permission from PHP with chmod()

// 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