Home > Blockchain >  Saving generated sql file to PHP script folder
Saving generated sql file to PHP script folder

Time:01-20

I have a PHP script that generates a MySQL database backup.

Executing the PHP on a web browser, prompts the user to download the generated sql file.

Here you have the piece of code that generates the sql file and prompts to download the generated sql file:

if(!empty($sqlScript))
{
    // Save the SQL script to a backup file
    $backup_file_name = $database_name . '_backup_' . time() . '.sql';
    $fileHandler = fopen($backup_file_name, 'w ');
    $number_of_lines = fwrite($fileHandler, $sqlScript);
    fclose($fileHandler); 

    // Download the SQL backup file to the browser
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename=' . basename($backup_file_name));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($backup_file_name));
    ob_clean();
    flush();
    readfile($backup_file_name);
    exec('rm ' . $backup_file_name); 
}

I don´t need to download the file,what I need is to execute the PHP file every x days and save the generated sql file on the same server folder.

I can use cron jobs to launch the PHP file, my only need is to change above code in order to save the generated sql file on the same script folder.

CodePudding user response:

Remove everything that tries to download the file.

if(!empty($sqlScript))
{
    // Save the SQL script to a backup file
    $backup_file_name = $database_name . '_backup_' . time() . '.sql';
    file_put_contents($backup_file_name, $sqlScript);
}
  •  Tags:  
  • php
  • Related