php exec() - mysqldump creates an empty file

早过忘川 提交于 2019-12-01 03:07:13
javinczki

Try this one:

$command = 'd:\xampp\mysql\bin\mysqldump --opt -u '.$_user.' -p'.$_pass.' '.$_db.' > test.sql 2>&1';

-its about permission issue.

These are the parameters

-uROOT -pPASSWORD --databases DB --result-file=FILE.SQL

If you look at the manual for exec the output goes to an array that is the second argument. That may be the source of the problem.

You should remove the space between the -p and the password.

--opt isn't needed with recent versions of mysql.

Other than that, your syntax is correct so if it doesn't work with the -p fix, you should check the parameters and the produced command.

Take the variables out of the quotes and remove --opt.

Also make sure that you are having unique file names

$backupfile = $dbname . date("Y-m-d-H-i-s") . '.sql';

$command = "D:\xampp\mysql\bin\mysqldump -u $_user -p$_pass $_db > $backupfile";

system($command);

For those on Macs

I was battling this issue the entire evening today. Silly mistake: one needs to chmod the directory that you are dumping the file to.

I ran this which fixed the issue:

chmod -R 777 your_dump_directory/

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!