php exec() - mysqldump creates an empty file

后端 未结 7 1636
再見小時候
再見小時候 2021-01-11 14:22

I want to create a backup from a database, but I get only a blank file.

include(\'config.php\');

$command = \"mysqldump --opt -h \".$_host.\" -u \".$_user.\         


        
相关标签:
7条回答
  • 2021-01-11 15:23

    Try this:

    $DBUSER="user";
    $DBPASSWD="password";
    $DATABASE="DBname";
    
    $filename = "backup-" . date("d-m-Y") . ".sql";
    
    $command = '"C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqldump.exe" '.$DATABASE ." -u".$DBUSER ." -p".$DBPASSWD." > your_web_site/$filename";
    passthru($command);
    
    • Change the route to the direction of the mysqldump.exe application of your computer.
    • Respect the " " inside the command.

    Then force the download of the file:

    if (file_exists("your_web_site/".$filename)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="'.basename($filename).'"');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    readfile("your_web_site/".$filename);
    exit;
    }
    

    Edit: You have to give permissions to the folder where you keep copies.

    0 讨论(0)
提交回复
热议问题