来自脚本的格式错误的标题错误的标头= 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.
无论如何,因为移动服务器的人无法下载文件,因为它现在给出错误500错误,并在日志文件中带来此错误:
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:
来自脚本的格式错误的标头。错误的标头= 1:index.php
malformed header from script. Bad header=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;
}
现在如果我输出:
// 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.
我所看到的是标题不正确但在阅读了php.net以及其他网站上的内容后,这看起来很好。
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.
任何人都可以大声说出我为什么会收到这些错误吗?
Can anyone give a shout on why I am getting these errors?
谢谢
推荐答案
问题在于
header('Content-Length: ' . filesize($file) + 1);
在加号之前评估点,所以你的代码是字符串+ 1,结果是1,这导致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));
因为根据这个页面 http://www.php.net/manual/en/function.readfile.php 不使用+1。其他解决方案是
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));
可以根据需要使用。
这篇关于来自脚本的格式错误的标题错误的标头= 1:index.php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!