【发布时间】:2019-12-08 02:16:36
【问题描述】:
我正在编写一个用于备份数据库的php脚本,想法是使用mysqldump命令然后下载一个带有结果的sql文件,不幸的是mysqldump的输出没有保存在文件中,只是源代码。这是代码:
$archivo = 'bkbiblioteca_' . date("d-m-Y_H:i:s") . '.sql';
$comando = "mysqldump --add-drop-table --host=$servidor --user=$usuario --password=$clave $base > $archivo";
try{
$archivo_manejador = fopen($archivo, 'w+');
fwrite($archivo_manejador, $comando);
fclose($archivo_manejador);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . basename($archivo));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($archivo));
ob_clean();
flush();
readfile($archivo);
unlink($archivo);
}catch(\Exception $e){
echo 'error en el proceso de bk' . $e->getMessage(); //TODO: favor incluir en la bitacora global del sistema
}//fin de catch
现在我正在使用以下行获取文件: mysqldump --add-drop-table --host=$servidor --user=$usuario --password=$clave $base > $archivo
我试过这个命令:
system("$comando");
但是它不起作用,所以我无法弄清楚如何执行mysqldump命令并在文件中获取结果而不是获取源代码行。
提前致谢
【问题讨论】:
-
目前,您只是将
$comando写入$archivo。试试系统调用,去掉$archivo_manejador这行,然后查看$archivo的内容 -
你得到了什么 echo exec("$comando") ?你是从 CLI 执行你的 PHP 脚本吗?
-
注释 $archivo_manejador = fopen($archivo, 'w+') 行解决了问题,现在我得到了所需的输出。谢谢弗朗索瓦