Boot to root and after deleting files from the database

I have this function and it automatically deletes text files after a certain age from my database.

$r = new textfiles;
$db = new DB;
$currTime_ori = $db->queryOneRow("SELECT NOW() as now");

...

if($this->site->textfilesretentiondays != 0)
        {
            echo "PostPrc : Deleting textfiles older than ".$this->site->textfilesretentiondays." days\n";

            $result = $db->query(sprintf("select ID from textfiles where postdate < %s - interval %d day", $db->escapeString($currTime_ori["now"]), $this->site->textfilesretentiondays));      
            foreach ($result as $row)
                $r->delete($row["ID"]);
        }

Now I would edit this function so that first all the text files are automatically loaded into the root directory /www/backup, and then the script should delete the text files with the line$r->delete($row["ID"]);

At the moment, I have no idea how to implement this.

+4
source share
3 answers

It seems impossible for me to give you a complete answer to your question, because there is a leak of information.

? , "$row", .


(, , ), "copy" (http://php.net/manual/de/function.copy.php) php . , , , script -, .

textfiles makeBackup.

+3

, . , .txt json_encoded , , FOREACH, delete:

$myfile = fopen("/www/backup/".$row["ID"].".txt", "w") or die("Unable to open file!");
$txt = json_encode($row);
fwrite($myfile, $txt);
fclose($myfile);
+1

According to your approach ..

function delete ($id){
   $result = $db->query(sprintf("select * from textfiles where id=$id);

   //if you have filepath use copy as SebTM suggested
   $path = $row['path']; //assuming path is the column name in ur db
   $filename = basename($path); //to get filename
   $backup_location = '/www/backup/'.$filename;
   copy($path, $backup_location);

   //if you have data in db
   $content = $row['data'] //assuming data to be backed up to file is in a field 'data'
   $backup_location = '/www/backup/file.txt';
   file_put_contents($backup_location, $content);
}

But this is not the most optimal approach, you can even transfer the initial request to the delete function above and call the delete function only once, instead of calling it in a loop.

+1
source

Source: https://habr.com/ru/post/1628457/


All Articles