Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm having trouble trying to build a page that let's someone download a .csv file containing data which is set in a variable from the frontend.

My code is below, but the only thing that is happening is that the data is being shown in the page, but there is no prompt to download the .csv file.

Can anyone see what the problem might be?

Thanks

Osu

<?php 
/**
 * CSV page template
 *
 */
?>

<?php get_header(); ?>

        <div id="sidebar-1">
            <?php get_sidebar(); ?>
        </div> <!-- End div#sidebar -->

        <div id="primarycontent">
            <?php get_template_part( '/loop', 'default' ); ?>

            <h2>Export</h2>

            <form action="" method="post">
                <input name="submit" type="submit" value="Export">
                <input type="hidden" name="submit" />
            </form>

            <?php
            // Check if form submitted before
            if($_SERVER['REQUEST_METHOD'] == "POST") {

                $fileName = date("d-m-y") . '-bv-directory.csv';
                $content = ""; // content added below

                // Title of the CSV
                $content = "Name,Address,Age,Phone \n";

                // Data in the CSV
                $content .= "\"John Doe\",\"New York, USA\",15,65465464 \n";

                // Create csv and force download
                header('Content-Type: application/csv'); 
                header("Content-length: " . filesize($NewFile)); 
                header('Content-Disposition: attachment; filename="' . $fileName . '"'); 
                echo $content;
            } ?>

        </div> <!-- End div#primarycontent -->

<?php get_footer(); ?>
share|improve this question

1 Answer 1

up vote 2 down vote accepted

Put all the code that handles the output of the CSV before your get_header() call, then exit:

<?php 

/**
 * CSV page template
 *
 */

if($_SERVER['REQUEST_METHOD'] == "POST") {

    $fileName = date("d-m-y") . '-bv-directory.csv';
    $content = ""; // content added below

    // Title of the CSV
    $content = "Name,Address,Age,Phone \n";

    // Data in the CSV
    $content .= "\"John Doe\",\"New York, USA\",15,65465464 \n";

    // Create csv and force download
    header('Content-Type: application/csv'); 
    header("Content-length: " . filesize($NewFile)); 
    header('Content-Disposition: attachment; filename="' . $fileName . '"'); 
    echo $content;

    exit;
}

?>

<?php get_header(); ?>

    <div id="sidebar-1">
        <?php get_sidebar(); ?>
    </div> <!-- End div#sidebar -->

    <div id="primarycontent">
        <?php get_template_part( '/loop', 'default' ); ?>

        <h2>Export</h2>

        <form action="" method="post">
            <input name="submit" type="submit" value="Export">
            <input type="hidden" name="submit" />
        </form>

    </div> <!-- End div#primarycontent -->

<?php get_footer(); ?>
share|improve this answer
    
TheDeadMedic - You're fantastic, that totally worked. Thanks! –  Osu Aug 1 '12 at 13:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.