当mysql工作时,我使用此代码来获取数据库的备份:
if (isset($_POST['getbackup'])) {
$db = "newsite";
$version = "2.3.1";
$date = date('Y-m-d H:i:s');
$final = "-- #".$version."#\n";
$final .= "-- database backup\n";
$final .= "--\n";
$final .= "-- PHP version: ".phpversion()."\n";
$final .= "-- MySQL version: ".mysqli_get_server_info($mysqli)."\n";
$final .= "-- Date: ".date("r")."\n";
$result = $mysqli->query("SHOW TABLE STATUS FROM ".$db);
while ($table = $result->fetch_array()) {
$i = 0;
$result2 = $mysqli->query("SHOW COLUMNS FROM $table[0]");
$z = $result2->num_rows;
$final .= "\n--\n-- DB Export - Table structure for table `".$table[0]."`\n--\n\nCREATE TABLE `".$table[0]."` (";
$prikey = false;
$insert_keys = null;
while ($row2 = $result2->fetch_array()) {
$i++;
$insert_keys .="`".$row2['Field']."`";
$final .= "`".$row2['Field']."` ".$row2['Type'];
if($row2['Null'] != "YES") { $final .= " NOT NULL"; }
if($row2['Default']) $final .= " DEFAULT '".$row2['Default']."'";
if($row2['Extra']) { $final .= " ".$row2['Extra']; }
if($row2['Key'] == "PRI") { $final .= ", PRIMARY KEY (`".$row2['Field']."`)"; $prikey = true; }
if($i < $z){
$final .= ", ";
$insert_keys .=", ";
}
else{
$final .= " ";
}
}
if($prikey) {
if($table[10]) $auto_inc = " AUTO_INCREMENT=".$table[10];
else $auto_inc = " AUTO_INCREMENT=1";
}
else $auto_inc = "";
$charset = explode("_", $table[14]);
$final .= ") ENGINE=".$table[1]." DEFAULT CHARSET=".$charset[0]." COLLATE=".$table[14].$auto_inc.";\n\n--\n-- DB Export - Dumping data for table `".$table[0]."`\n--\n";
$inhaltq = $mysqli->query("SELECT * FROM $table[0]");
while($inhalt = $inhaltq->fetch_array()) {
$final .= "\nINSERT INTO `$table[0]` (";
$final .= $insert_keys;
$final .= ") VALUES (";
for($i=0;$i<$z;$i++) {
$inhalt[$i] = str_replace("'","`", $inhalt[$i]);
$inhalt[$i] = str_replace("\\","\\\\", $inhalt[$i]);
$einschub = "'".$inhalt[$i]."'";
$final .= preg_replace('/\r\n|\r|\n/', '\r\n', $einschub);
if(($i+1)<$z) $final .= ", ";
}
$final .= ");";
}
$final .= "\n";
}
if($logged) {
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Description: File Transfer");
if(is_integer(mb_strpos(strtolower($_SERVER["HTTP_USER_AGENT"]), "msie")) AND is_integer(mb_strpos(strtolower($_SERVER["HTTP_USER_AGENT"]), "win" ))) header("Content-Disposition: filename=backup-".strtolower(date("D-d-M-Y")).".sql;");
else header("Content-Disposition: attachment; filename=backup-".strtolower(date("D-d-M-Y")).".sql;");
header("Content-Transfer-Encoding: binary");
}
}
它现在也可以使用,当我点击按钮时我得到一个文件下载,但是文件中没有SQL文件,而是页面的源代码在文件中..
我做错了什么?
答案 0 :(得分:1)
您没有输出$final
数据。我正在使用这段代码做同样的事情,试试吧......
header('Content-Description: File Transfer');
header('Content-type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $backup_filename . '.sql');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . strlen($final));
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Expires: 0');
header('Pragma: public');
echo $final;