简体   繁体   English

来自脚本的格式错误的标题 错误的标头= 1:index.php

[英]malformed header from script. Bad header=1: index.php

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 500 error and in the log files it is bringing this error: 无论如何,因为移动服务器的人无法下载文件,因为它现在给出错误500错误,并且在日志文件中它带来了这个错误:

malformed header from script. 来自脚本的格式错误的标题 Bad header=1: index.php 错误的标头= 1:index.php

The only code which is related to this which I can see anyway is this: 我能看到的与此相关的唯一代码是:

// Echo $productOptionDetails->file;                
$file = DOWNLOAD_FOLDER. '/'. $productOptionDetailEntity->file;

    if (file_exists($file)) {

        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($file));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file) + 1);
        ob_clean();
        flush();
        readfile($file);
        exit;
    }

Now if I just output: 现在如果我输出:

// Echo $productOptionDetails->file;                
$file = DOWNLOAD_FOLDER. '/'. $productOptionDetailEntity->file;

if (file_exists($file)) {
   readfile($file);
   exit;
}

It outputs lots of encrypted text so its obviously reading something. 它会输出大量加密文本,因此它显然可以阅读。

What I have read is that the headers are incorrect but after reading loads of content in php.net as well as other websites this looks fine. 我读过的是标题不正确但在阅读了php.net以及其他网站中的内容后,这看起来很好。

Can anyone give a shout on why I am getting these errors? 任何人都可以大声说出我为什么会收到这些错误?

Thanks 谢谢

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. 在加号之前评估点,因此您的代码是字符串+ 1,结果是1,这导致1作为标题发送。 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. 因为根据这个页面http://www.php.net/manual/en/function.readfile.php没有使用+1。 Other solution is 其他解决方案是

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

which will work as you want. 这将按你的意愿工作。

Based on what you said, the problem is because there is something you said to echo before you wrote the code to download. 基于你所说的,问题是因为在你编写下载代码之前,有些东西你说过要echo 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. 要解决此问题,您应该编写代码,以便在下载代码执行时应在下载代码之后写入echo

It may not always an echo , it may be some HTML tags instead of it. 它可能并不总是echo ,它可能是一些HTML tags而不是它。

Download should be the first thing to be run in any page if you want to download something from that page to clients' system. 如果您想从该页面下载某些内容到客户的系统,那么下载应该是在任何页面中运行的第一件事。

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. 这是一个常见的问题,在启动php标签之前会放置一些字符,如空格,新行甚至是UTF-8格式错误的BOM(字节顺序标记)。

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(); 另外,ob_clean(); and flush(); 和flush(); functions are not needed in you code. 代码中不需要函数。

As far as I can understand from the code flush() is not needed. 据我所知,代码中不需要flush() Also instead of using 而不是使用

ob_clean();
flush();

just use ob_end_clean() to clear any buffered code. 只需使用ob_end_clean()清除任何缓冲的代码。

And you said you were moving servers and now it is not working, does it means you have modified PHP settings or version too? 并且你说你正在移动服务器,现在它不起作用,这是否意味着你已经修改了PHP设置或版本?

If yes than do post the PHP configuration for output_buffering . 如果是,则发布用于output_buffering的PHP配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM