Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 强制文件“另存为”浏览器窗口_Php_Mysql - Fatal编程技术网

Php 强制文件“另存为”浏览器窗口

Php 强制文件“另存为”浏览器窗口,php,mysql,Php,Mysql,借助下面给出的这个函数,我想将数据库表的转储保存在记事本上,但它不允许我保存它,而是始终在浏览器上显示数据。我应该如何保存它???在函数下面,我还发布了函数调用代码。也检查一下 提前多谢 //function for making .sql file (dump) public function backup_tables($host,$user,$pass,$name,$tables = '*') { $link = mysql_connect($host,$user,

借助下面给出的这个函数,我想将数据库表的转储保存在记事本上,但它不允许我保存它,而是始终在浏览器上显示数据。我应该如何保存它???在函数下面,我还发布了函数调用代码。也检查一下

提前多谢

//function for making .sql file (dump)

    public function backup_tables($host,$user,$pass,$name,$tables = '*')

{

     $link = mysql_connect($host,$user,$pass);

     mysql_select_db($name,$link);

    //get all of the tables

    if($tables == '*')
    {
        $tables = array();

        $result = mysql_query('SHOW TABLES');

        print_r( $result);

        //die('result');

        while($row = mysql_fetch_row($result))

        {
            $tables[] = $row[0];
        }

    }

    else

    {
        $tables = is_array($tables) ? $tables : explode(',',$tables);
    }

    //cycle through

    foreach($tables as $table)

    {

        $result = mysql_query('SELECT * FROM '.$table);

        $num_fields = mysql_num_fields($result);

        //$return.= 'DROP TABLE '.$table.';';

        $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));

        $return.= /*"\n\n".*/$row2[1].";\n\n";

        for ($i = 0; $i < $num_fields; $i++) 
        {
            while($row = mysql_fetch_row($result))
            {
                $return.= 'INSERT INTO '.$table.' VALUES(';

                for($j=0; $j<$num_fields; $j++) 

                {
                    $row[$j] = addslashes($row[$j]);

                    $row[$j] = ereg_replace("\n","\\n",$row[$j]);

                    if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
                    if ($j<($num_fields-1)) { $return.= ','; }

                }
                $return.= ");\n";

            }
        }

        $return.="\n\n\n";

    }

    //save file

    $handle = fopen('./db_files/db-backup-'.date('d-m-Y_H-i-s').'-'.(implode(',',$tables)).'.sql','w+');

    header("Content-type:application/sql");

    header('Content-Disposition: attachment; filename="'.$filename.'" ');

    fwrite($handle,$return);

    fclose($handle);

}   
    // end of function for making .sql file (dump)


//call to a function

$this->backup_tables('host','user','pass','db name','table name');
:如果希望提示用户保存正在发送的数据,例如生成的PDF文件,可以使用»Content Disposition标头提供建议的文件名,并强制浏览器显示保存对话框

<?php
header("Cache-Control: must-revalidate");
header("Pragma: must-revalidate");

header("Content-type: text/csv");
header("Content-disposition: attachment; filename=$filename.csv");
// Your code here
?>
:如果希望提示用户保存正在发送的数据,例如生成的PDF文件,可以使用»Content Disposition标头提供建议的文件名,并强制浏览器显示保存对话框

<?php
header("Cache-Control: must-revalidate");
header("Pragma: must-revalidate");

header("Content-type: text/csv");
header("Content-disposition: attachment; filename=$filename.csv");
// Your code here
?>

在某些情况下,你不能强迫它

这取决于浏览器设置。例如,在firefox->preferences中,有一个选项可供选择:

将文件保存到__

每次都要问

因此,如果用户选择了“将文件保存到”选项,则无论您在PHP代码中执行何种编程,此“另存为”窗口都不会打开

如果您想要一些示例标题,可以在输出任何内容之前在php文件中包含这些标题:

header("Content-type: text/csv");
header("Content-disposition: attachment; filename=$filename.csv");

在某些情况下,你不能强迫它

这取决于浏览器设置。例如,在firefox->preferences中,有一个选项可供选择:

将文件保存到__

每次都要问

因此,如果用户选择了“将文件保存到”选项,则无论您在PHP代码中执行何种编程,此“另存为”窗口都不会打开

如果您想要一些示例标题,可以在输出任何内容之前在php文件中包含这些标题:

header("Content-type: text/csv");
header("Content-disposition: attachment; filename=$filename.csv");

将内容类型设置为内容类型:应用程序/强制下载,它将调用“另存为”对话框。顺便说一句,您不输出sql代码,只需将其保存为fwrite$handle,$return;如果要输出,请添加echo$return;application/force download不是有效的MIME类型,在任何情况下都不应使用。将content type设置为content type:application/force download,它将调用“另存为”对话框。顺便说一句,您不输出sql代码,只需将其保存为fwrite$handle,$return;如果要输出,请添加echo$return;application/force download不是有效的MIME类型,在任何情况下都不应使用。这会导致调用函数,但不会给出正确的sq.格式。事实上,我是php新手,这就是为什么我太困惑的原因。我应该怎么做才能让一切都完全正常$this->backup_tables'host'、'user'、'password'、'database_name'、'table name';头部疼痛控制:必须重新验证;HeaderFragma:必须重新验证;headerContent类型:text/sql;校长内容倾向:依恋;文件名='。$filename';这是对函数的调用,但没有给出正确的sq.格式。事实上,我是php新手,这就是为什么我太困惑的原因。我应该怎么做才能让一切都完全正常$this->backup_tables'host'、'user'、'password'、'database_name'、'table name';头部疼痛控制:必须重新验证;HeaderFragma:必须重新验证;headerContent类型:text/sql;校长内容倾向:依恋;文件名='。$filename';