Home » Wordpress » SQL Export – WordPress plugin DEV

SQL Export – WordPress plugin DEV

Posted by: admin November 30, 2017 Leave a comment

Questions:

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!

Answers:

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.

Questions:
Answers:

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.