malformed header from script. Bad header=1: index.php

后端 未结 4 979
醉梦人生
醉梦人生 2021-01-18 20:22

I have a website which is a few years old now, it basically offers downloads.

Anyway since moving server people can not download files because its now giving a error

相关标签:
4条回答
  • 2021-01-18 20:58

    Based on what you said, the problem is because there is something you said to echo before you wrote the code to download. To overcome this problem you should write the code so as the echo should be written after the download code at the time the download should be get performed.

    It may not always an echo, it may be some HTML tags instead of it.

    Download should be the first thing to be run in any page if you want to download something from that page to clients' system.

    0 讨论(0)
  • 2021-01-18 20:59

    The problem is in

    header('Content-Length: ' . filesize($file) + 1);
    

    Dot is evaluated before plus so your code is string + 1, result is 1 and this causes that 1 is sent as header. Correct code should be

    header('Content-Length: ' . filesize($file));
    

    because according to this page http://www.php.net/manual/en/function.readfile.php no +1 is used. Other solution is

    header('Content-Length: ' . (filesize($file) + 1));
    

    which will work as you want.

    0 讨论(0)
  • 2021-01-18 21:05

    As far as I can understand from the code flush() is not needed. Also instead of using

    ob_clean();
    flush();
    

    just use ob_end_clean() to clear any buffered code.

    And you said you were moving servers and now it is not working, does it means you have modified PHP settings or version too?

    If yes than do post the PHP configuration for output_buffering.

    0 讨论(0)
  • 2021-01-18 21:08

    This problem is related to some output which appears before you send headers.

    It is a common problem that some characters like spaces, new lines or even UTF-8 malformed BOM (Byte order mark) are placed before starting php tag.

    You need to be sure that absolute nothing is echo-ed before sending headers and need to check that for all included files in your script.

    In addition, ob_clean(); and flush(); functions are not needed in you code.

    0 讨论(0)
提交回复
热议问题