1

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

jeddey
  • 63
  • 2
  • 12
  • 2
    possible duplicate of [How to fix "Headers already sent" error in PHP](http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) – Siim Kallari Apr 10 '15 at 08:41

2 Answers2

4

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

Miglen.com
  • 339
  • 1
  • 7
  • thank you for answer , i did try to switch the inclusion case but i got error 500 internal server error i didnt understand why ....... :| , i have alrady printed table of the data that i will export in csv – jeddey Apr 10 '15 at 09:02
  • try with the `header_remove(); ` it might help in your case, but i'm positive that there is some output before your statements. – Miglen.com Apr 10 '15 at 09:03
  • db->prepare('SELECT * FROM bucatari_vmupdate.connection'); $d->execute(); => as you can see this is the begining of my code , but also didnt resolve the porblem , i still see the csv file as plain text when i click to export the file – jeddey Apr 10 '15 at 09:09
  • Can you test out with your browser to see the actual headers (Chrome -> F12 -> Click on export and see Network tab with your request). There are two options - something is printed or set already or there is space in between some tags. What do you have on line 78 in index.php ? – Miglen.com Apr 10 '15 at 09:18
  • in line 78 on index.php i have a
    , the function header_list() on my index.php?link=export.php give me this result Array ( [0] => X-Powered-By: PHP/5.4.39 [1] => Content-type: text/html ) i think that i need to change the content type in the export here or i dont know !!!! :| im alrady stacked with this
    – jeddey Apr 10 '15 at 09:22
  • Try `headers_remove("X-Powered-By");` and `headers_remove("Content-type");`. This is really strange. – Miglen.com Apr 10 '15 at 09:24
  • i did try it , all page content not appear – jeddey Apr 10 '15 at 09:26
  • i take a screen shot here http://bucatarie360.ro/dev/dashboard/1.png , after removing header content-type , the page is not showing anything – jeddey Apr 10 '15 at 09:31
  • Howdy, neighbor from România. Other option is to move the export outside of export.php in the beginning of index.php, or to give me a sneak into your index.php and I can give you details. stackoverflow at miglen.com is my mail. – Miglen.com Apr 10 '15 at 09:34
0

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');
.
.
.
.
}
Hassan Ali Shahzad
  • 2,439
  • 29
  • 32