PHP & Wordpress: headers already sent - what to do

2019-09-19 13:22发布

This question already has an answer here:

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

标签: php wordpress
2条回答
手持菜刀,她持情操
2楼-- · 2019-09-19 14:16

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.

查看更多
霸刀☆藐视天下
3楼-- · 2019-09-19 14:20

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.

查看更多
登录 后发表回答