3

嗨所以我有以下代码..

function export_file() {


    $tmpname = '/tmp/' . sha1( uniqid() ) . $ext;
    $filename = 'export.sql';
    $cmd = sprintf( "/Applications/XAMPP/xamppfiles/bin/mysqldump -h'%s' -u'%s' -p'%s' %s --single-transaction %s > %s",
        DB_HOST, DB_USER, DB_PASSWORD, DB_NAME, $compression_pipe, $tmpname );

    exec( $cmd );
    header( 'Content-Type: application/bzip' );
    header( 'Content-Length: ' . filesize( $tmpname ) );
    header( 'Content-Disposition: attachment; filename="' . $filename . '"' );
    readfile( $tmpname );
    unlink( $tmpname );

    exit();
}

现在,它可以工作但不下载文件。说标题已经发送。

已发送的标头(输出开始于 /Applications/XAMPP/xamppfiles/htdocs/~/wp-admin/includes/template.php:1642)

现在我知道标头已经从我的主插件文件发送到包含我正在创建的插件所需的 javascript 和 css 文件。

 function admin_register_head() {
$siteurl = get_option('siteurl');
$css = $siteurl . '/wp-content/plugins/' . basename(dirname(__FILE__)) . '/style.css';
$script = $siteurl . '/wp-content/plugins/' . basename(dirname(__FILE__)) . '/script.js';
echo "<link rel='stylesheet' type='text/css' href='$css' />"; 
echo "<script src='$script' type='application/javascript'>"; }

我如何告诉它使用这些标题再次加载页面,以便我的 sql 转储作为文件下载而不是显示/在服务器上创建的文件。例如。按下链接或按钮并调用此函数并添加这些标头以便将 sql 转储下载为文件?如果我在这里完全偏离轨道,请纠正我。接受有关如何执行此操作的建议。

I am a noob and I have learnt a hell of a lot so far with PHP, mySQL and wordpress creating the different elements to this plugin. Your help will be much appreciated. I have searched an searched, read tutorials but just can't seem to get it to work.

4

2 回答 2

1

I did figure this out just now.

if ( isset( $_GET['sql_export_export'] ) ) {
            export_file(); }

function export_file() {


        $tmpname = '/tmp/' . sha1( uniqid() ) . $ext;
        $filename = 'export.sql';
        $cmd = sprintf( "/Applications/XAMPP/xamppfiles/bin/mysqldump -h'%s' -u'%s' -p'%s' %s --single-transaction %s > %s",
            DB_HOST, DB_USER, DB_PASSWORD, DB_NAME, $compression_pipe, $tmpname );

        exec( $cmd );
        header( 'Content-Type: application/bzip' );
        header( 'Content-Length: ' . filesize( $tmpname ) );
        header( 'Content-Disposition: attachment; filename="' . $filename . '"' );
        readfile( $tmpname );
        unlink( $tmpname );

        exit();
    }


    function database_backup() {


    ?><a class="button" href="?sql_export_export">Export SQL</a><?php

    } 

I am using xampp on my local to build this plugin for WP, so replace the mysqldump path to suit your server.

于 2013-08-20T23:33:01.013 回答
0

It depends a little how your export_file() function is being called. But if it's called directly from an admin URL, you can add &noheader=1 to the URL. I use that successfully when generating an Excel file.

于 2013-08-20T07:53:14.033 回答