0

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

Slider345
  • 4,558
  • 7
  • 39
  • 47
  • 1
    Are you sure you don't have any spaces at the beginning or end of your script? Newlines? – Waynn Lue Apr 17 '12 at 01:37
  • Your script seems to work fine when I tested it on my dev server, so there's nothing wrong with the code. Check any spaces as @WaynnLue suggested. You can also remove the `?>` as it is not required and can sometime inject whitespaces. – F21 Apr 17 '12 at 01:41
  • Yeah there are no spaces! Is it possible there is some hidden new lines? I'm editing on windows, in Visual Studio. – Slider345 Apr 17 '12 at 01:43
  • 2
    Use notepad.....or a proper PHP editor – Lawrence Cherone Apr 17 '12 at 01:54
  • 1
    Possibly a unicode BOM at the start of the file. They'd be 2 invisible bytes before the `` portion, and count as output. – Marc B Apr 17 '12 at 02:21
  • Copied the script into Notepad, uploaded, and it worked. aargh – Slider345 Apr 17 '12 at 04:55

1 Answers1

2

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

Waynn Lue
  • 11,344
  • 8
  • 51
  • 76