PHP & Wordpress: headers already sent - what to do

2019-09-19 13:54发布

问题:

This question already has an answer here:

  • How to fix “Headers already sent” error in PHP 11 answers

i'm working on a wordpress template where files of my ftp get listed. if i click on a file I call d.php (d=download) where i simply pass along the path of the file and I want to force a download with

header('Content-Disposition: attachment; filename="'. $file .'"');

This is not working for me, because the browser keeps telling me:

Warning: Cannot modify header information - headers already sent by (output started at /clientdata/n5200-2-dynamic/a/l/mydomain.net.au/www/wp-content/themes/mytheme/d.php:25) in /clientdata/n5200-2-dynamic/a/l/mydomain.net.au/www/wp-content/themes/mytheme/d.php on line 45

any idea what I could do here? The d.php doesn't even include any wordpress specific stuff (get_header() or get_footer()) - it's just a simple php file in the template directory of my theme. Any idea what I could do here?

thank you

回答1:

When using the header() function in PHP, it must be sent before any output is sent. This means that it must be executed before the <html> tag. This will mean including it in a procedurally earlier script.



回答2:

I believe WordPress routes everything thru index.php, so technically you are wrapped inside of the WordPress stuff.

Regardless, something is sending the headers before you are. This could be white space, PHP output, or indeed the actual headers.



标签: php wordpress