tags:

views:

217

answers:

4

I have an issue, my application works as follows.

First echo to the user that the generating of the csv file has started.

eg. echo 'Building of csv file started';

then I build the csv file.

Now I want to force the file to download, and this works perfectly.

this is my code

    header('Content-Type: application/csv');
    header('Content-Disposition:attachment;filename='.$year.'_'.$month.'_'.'export.csv');
    header('Pragma: no-cache');

    echo readfile($myFile);

No the first echo 'Building of csv file started' gets included in the CSV file and I do not know how to exlude it.

+3  A: 

The solution is really easy. As you want two different header types you have to create 2 different pages.

The first page displaying "Building of csv file started" and containing an iframe with your second page and a second page with your real csv file and the attachment header.

Ghommey
Simply redirecting to the CSV download page could also work.
Ates Goral
Yes a javascript redirect would work too however it requieres you to have javascript activated. A HTTP redirect won't work as he wants to display a message and to offer the download.
Ghommey
A: 

Anything you echo is going to go into the CSV. You can't both echo and output a CSV in the same script.

ceejayoz
+1  A: 

You cannot force anything to download over HTTP. That is not how HTTP works. If you want something to download then it must be an asset in response to a user request. This means that the user must do something to trigger a request for the download explicitly or the download must be in response to an AJAX request.

A: 

A user is on your page - they click the "build CSV" link. Then using some very, very basic JS you can show a message/modal/pop-up stating "Building of csv file started..." back on the server side PHP you can remove that echo from the page that actually generates the CSV and the user will be prompted with the file.

Xeoncross