0

I'm struggling to get a dynamically-generated CSV to download with a template_redirect. It seems to work (if not this exact version, another similar version) when it does not go through Wordpress, e.g if I just add a file like http://www.example.com/phpfile.php. If you open the redirect page it will display the CSV as text in the browser but if I make a link to the redirect and click "save as..." it cannot find a file so does not download. No matter which headers I try I cannot get it to download successfully with Chrome... and it has to work on all browsers.

This is the code I have but I have been trying different combinations of headers and different ways of displaying the content. I have also tried using the add_rewrite_endpoint hook. Here is the code...

function my_download_redirect() {

    if ($_SERVER['REQUEST_URI']=='/download-csv/data.csv') {
        $csv = '"what",';
        $csv .= '"is",';
        $csv .= '"your",';
        $csv .= '"name",';
        $csv .= PHP_EOL;
        $csv .= '"hello",';
        $csv .= '"my",';
        $csv .= '"name",';
        $csv .= '"is",';
        $csv .= PHP_EOL;
        $length = strlen($csv);

        ob_clean(); 

        header('Content-Disposition: attachment; filename="data.csv"');
        header("Content-Type: application/force-download",true,200);
        header("Content-Length: " . $length);
        header("Pragma: no-cache");
        header("Expires: 0");
        header("Connection: close");

//          $csv_output = fopen('php://output', 'w');
//          fwrite($csv_output, $csv);
//          fclose($csv_output);
        echo $csv;
        exit();
    }


}
add_action( 'template_redirect', 'my_download_redirect' );

Maybe I have a security plugin blocking it or maybe Wordpress is blocking it or maybe I'm just doing it wrong. I don't want to have this functionality if it's not going to work for 50% of cases.

Am I doing something wrong?

If not, is this the kind of thing that is blocked by security plugins? Or maybe more recent versions of Wordpress don't allow it?

Any comments much appreciated. Thanks in advance.

  • 1
    For what it's worth, the code works for me exactly as posted in Chrome, Version 56.0.2924.76 (64-bit) WP v4.7.2. When I visit the URL or do a (right click, download on the link) http://example.com/download-csv/data.csv the browser prompts me to download the file. Also, I did run into an issue trying to upload a CSV in WP v.4.7.2. I know you're dynamically generating the file, but during testing I ran into this issue so I thought I'd make note of it here: wordpress.org/support/topic/… – Dave Romsey Feb 1 '17 at 22:47
  • 1
    @dave-romsey Thank you very much, very interesting. I've probably gone crazy with Wordpress hardening! At least now I know I just have to figure out which settings to turn off and it should work. – sdexp Feb 1 '17 at 23:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.