0

I'm trying to create a function that allows users to download files. When I click a button to download the file this function is called and I'm getting the "Cannot modify header information - headers already sent by" error.

From what I know, the "headers already sent..." error is caused by whitespace, but I can't seem to find anything. Is there something I'm not taking account of? This is NOT the only function on the page where it's located. Do I have to load the page a special way?

function serveFile($fileID){
    $sql = mysql_query("select filename from files where id = '$fileID'");
    $file = mysql_fetch_array($sql);

    header('Content-type: text/plain');

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

    readfile('/user_files/'.$fileID.' ');
}

Update - I got it to work, thanks for the tips. I moved the function call to the top of my page.

user1406951
  • 454
  • 1
  • 10
  • 28
  • is this the **top** of your file? If you have ANY html or any characters printing before this you'll get the error. – d-_-b Sep 18 '12 at 16:34
  • 1
    That error can also be caused by any other output, for example, PHP throwing up error messages. Please stop writing new code with the ancient mysql_* functions. They are no longer maintained and community has begun the [deprecation process](http://news.php.net/php.internals/53799). Instead you should learn about prepared statements and use either [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). If you care to learn, [here is a quite good PDO-related tutorial](http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers). – DCoder Sep 18 '12 at 16:34
  • @iight no, this is not at the top of my file. I'm assuming I should move it? This is part of a class, will it still work considering there are a few things I have to declare? – user1406951 Sep 18 '12 at 16:35
  • there must be something which is printing some values.. might be normal text or warning or anything before your header function call – Surace Sep 18 '12 at 16:36
  • @user1406951 anything written (i.e. `echo "something"` or any html or any anything, before this will cause the error. – d-_-b Sep 18 '12 at 16:36
  • @iight the page has already loaded by the time this function is called. The user presses a button and then this function is called. – user1406951 Sep 18 '12 at 16:37
  • I don't understand how if the page is already downloaded that the user clicking a button could call that function? Are you calling this within a script that is accessed via AJAX? – Mike Brant Sep 18 '12 at 16:42
  • @MikeBrant sorry, I updated it above. I'm used to working with Javascript...forgot it's server side. My mistake. Regardless, it's still not working properly. – user1406951 Sep 18 '12 at 16:43
  • 1
    The error message well tell you the line where output was already sent from? What does that say? Also the most be some other script calling this function, as it would not call itself. Check within that code for possible output. – Mike Brant Sep 18 '12 at 16:45
  • @iight I moved the function call to the top of the page and it worked. Thanks guys! – user1406951 Sep 18 '12 at 16:56

1 Answers1

2

Check if you have any whitespace in the HTML file.

All it takes is one space before you start spitting out your headers for this problem.

See this for lots more detail: How to fix "Headers already sent" error in PHP

Community
  • 1
  • 1
Snowcrash
  • 80,579
  • 89
  • 266
  • 376