使用php脚本导出mysql db仅在本地服务器上有效,而不在实时服务器上有效

使用php脚本导出mysql db仅在本地服务器上有效,而不在实时服务器上有效,php,Php,我正在尝试使用php脚本将db导出到.sql文件,它在本地运行良好,就像我单击“链接文件下载到默认下载路径”时一样,但当我在live server上尝试时,它会在网页上显示文本,而不会下载。我曾尝试删除php之后的所有空格,以为这可能是个问题,但问题仍然存在 下面是我使用的代码 function EXPORT_TABLES($host,$username,$password,$db,$tables=false, $backup_name=false ){ $mysqli = ne

我正在尝试使用php脚本将db导出到.sql文件,它在本地运行良好,就像我单击“链接文件下载到默认下载路径”时一样,但当我在live server上尝试时,它会在网页上显示文本,而不会下载。我曾尝试删除php之后的所有空格,以为这可能是个问题,但问题仍然存在

下面是我使用的代码

    function EXPORT_TABLES($host,$username,$password,$db,$tables=false, $backup_name=false ){
    $mysqli = new mysqli($host,$username,$password,$db);
    $mysqli->select_db($db);
    $mysqli->query("SET NAMES 'utf8'");

    $queryTables = $mysqli->query('SHOW TABLES');
    while($row = $queryTables->fetch_row()){
        $target_tables[] = $row[0];
    }
    if($tables !== false){
        $target_tables = array_intersect( $target_tables, $tables);
    }
    foreach($target_tables as $table){
        $result = $mysqli->query('SELECT * FROM '.$table);
        $fields_amount=$result->field_count;
        $rows_num=$mysqli->affected_rows;
        $res = $mysqli->query('SHOW CREATE TABLE '.$table);
        $TableMLine=$res->fetch_row();
        $content = (!isset($content) ?  '' : $content) . "\n\n".$TableMLine[1].";\n\n";
        for ($i = 0, $st_counter = 0; $i < $fields_amount;   $i++, $st_counter=0){
            while($row = $result->fetch_row()){//when started (and every after 100 command cycle):
                if ($st_counter%100 == 0 || $st_counter == 0 ){
                        $content .= "\nINSERT INTO ".$table." VALUES";
                    }
                $content .= "\n(";
                for($j=0; $j<$fields_amount; $j++){
                    $row[$j] = str_replace("\n","\\n", addslashes($row[$j]) );
                    if (isset($row[$j])){
                        $content .= '"'.$row[$j].'"' ;
                    }else {
                        $content .= '""';
                    }
                    if ($j<($fields_amount-1))
                    {
                        $content.= ',';
                    }
                }
                $content .=")";
                //every after 100 command cycle [or at last line] ....p.s. but should be inserted 1 cycle eariler
                if ( (($st_counter+1)%100==0 && $st_counter!=0) || $st_counter+1==$rows_num){
                    $content .= ";";
                }else{
                    $content .= ",";
                }
                $st_counter=$st_counter+1;
            }
        } $content .="\n\n\n";
    }
    $backup_name = $backup_name ? $backup_name : $db."___(".date('H-i-s')."_".date('d-m-Y').").sql";
    header('Content-Type: application/octet-stream');
    header("Content-Transfer-Encoding: Binary");
    header("Content-disposition: attachment; filename=\"".$backup_name."\"");
    echo $content;
    /*$handle = fopen($backup_name,'w+');
    fwrite($handle,$content);
    fclose($handle);
    echo 'done';*/
    exit;
}
函数导出\u表($host、$username、$password、$db、$TABLES=false、$backup\u name=false){
$mysqli=newmysqli($host、$username、$password、$db);
$mysqli->select_db($db);
$mysqli->query(“设置名称‘utf8’”);
$queryTables=$mysqli->query('showtables');
而($row=$queryTables->fetch_row()){
$target_tables[]=$row[0];
}
如果($tables!==false){
$target\u tables=array\u intersect($target\u tables,$tables);
}
foreach($target_tables作为$table){
$result=$mysqli->query('SELECT*FROM'.$table');
$fields\u amount=$result->field\u count;
$rows\u num=$mysqli->受影响的\u行;
$res=$mysqli->query('SHOW CREATE TABLE'.$TABLE);
$TableMLine=$res->fetch_row();
$content=(!isset($content)?“”:$content)。“\n\n”。$TableMLine[1]。;\n\n”;
对于($i=0,$st\u counter=0;$i<$fields\u amount;$i++,$st\u counter=0){
而($row=$result->fetch_row()){//启动时(以及每100个命令周期之后):
如果($st|U计数器%100==0 |$$st|U计数器==0){
$content.=“\n插入“$table.”值中;
}
$content.=“\n(”;

对于($j=0;$j),您可以为fwrite指定一个文件夹:

$folder = "temp";
if(!file_exists($folder)){          //Check if dir already exists
    mkdir($folder, 0777, true)      //Create one if it doesn't and set the file permission, adjust to your taste.
}

$handle = fopen($folder.'/'.$backup_name,'w+');
fwrite($handle,$content);
fclose($handle);
echo 'done';

如何将其保存到移动内存中的可能副本?是否可以使用php?