Minimal script--still getting Cannot modify header

2019-08-25 01:49发布

问题:

This question already has an answer here:

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

This is driving me crazy. It seems like a simple error--I've seen dozens of posts about their scripts are way more complicated then mine.

I have a php script which should force a download by setting the headers. I've slimmed the script down to this:

<?php
header("Content-Type: audio/mp3");
header("Content-Disposition: attachment; filename=\"audio.mp3\";" );
readfile("audio.mp3");
exit();
?>

Why are my headers getting sent out? I tried obj_start and obj_flush, but so far no luck. Could this be a problem with my host? I'm using 1and1.com

回答1:

Your script works fine when tested -- make sure you don't have any spaces at the beginning or end of your script.