dsafew1231 2013-08-20 02:49
浏览 93
已采纳

SQL导出 - Wordpress插件DEV

Hi So i have the following code..

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();
}

Now, it works but doesnt download the file. Saying headers already sent.

headers already sent by (output started at /Applications/XAMPP/xamppfiles/htdocs/~/wp-admin/includes/template.php:1642)

Now i understand that the headers have been sent from my main plugin file already to include javascript and css files that are needed for the plugin i am creating.

 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'>"; }

How do i go about telling it to load the page again with these headers so my sql dump downloads as a file and not displayed / file created on the server. eg. Press a link or button and to call this function and have those headers added so it downloads the sql dump as a file? please correct me if im totally off track here. Open to suggestions on how to do this.

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. Thanks in advanced!

  • 写回答

2条回答 默认 最新

  • doudie2693 2013-08-20 23:33
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计