简体   繁体   中英

headers already sent , export csv , can't download the file

im writing a basic script to download csv file based on database information, in my dashboard/index.php i use GET and switch to include pages so when i click on the link dashboard.php?link=export.php i have a table with the all the data , there i have a link that i can download my csv file , my problem is that when i click to export.csv , i have an text output and not download file so i put those code :

header("Content-type: text/csv");
header("Content-Disposition: attachment; filename={$filename}.csv");
header("Pragma: no-cache");
header("Expires: 0");

but always i see the content in text format and with an error

Warning: Cannot modify header information - headers already sent by (output started at /home/*/public_html/dev/dashboard/index.php:78) in /home/*/public_html/dev/dashboard/component/export.php on line 39

so i ask how can i resolve this issue , can i remove the header for the index in the export.php and set a new one also there to download the file or what extacly maybe i need to change just in the export.php the Content-Type to be text/csv but is alrady sent text/html . please help to resolve this thank you

As you can see, there is something already sent to output (printed) in your index.php file which is including your export.php file.

Make sure you are not printing anything before the headers. In some cases might be a space between the opening <?php tags or something little like that. btw mind that switch inclusion cases you have.

Other way is to try to use header_remove(); before the statements in export.php

Do not add anything before header in my case i was getting record from data base. If you have function to download csv the add following header at top in the function like:

function exportCsv($date) {
            header('Content-Type: text/csv; charset=utf-8');
            header('Content-Disposition: attachment; filename=data.csv');
.
.
.
.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM