apache 内容长度标头始终为零

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1334471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 18:06:15  来源:igfitidea点击:

Content-Length header always zero

phpapacheheadercontent-length

提问by rtacconi

I set a header in the following way:

我通过以下方式设置标题:

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

On my PC with ZendServer it works fine and I can download a file with the correct file size. On the production server, a Solaris with Apache and compiled PHP, I get a file with the file size equal to zero, so an empty file.

在装有 ZendServer 的 PC 上,它运行良好,我可以下载文件大小正确的文件。在生产服务器上,一个带有 Apache 和编译 PHP 的 Solaris,我得到一个文件大小为零的文件,所以是一个空文件。

Is there a config parameter? Something that can prevent to set 'Content-Length: 1222'?

有配置参数吗?可以阻止设置“内容长度:1222”的东西?

Thanks.

谢谢。

The code:

编码:

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

require 'includes/prepend.inc.php';
require __ADMIN_DIR__.'/AdminInfo.php';
$intFile = QApplication::QueryString('fileID');
if($intFile == '') die('Error: missing ID');
$objFile = File::Load($intFile);
$blnRight = false;
$objAdminInfo = new AdminInfo();
if($objAdminInfo->isAdmin()) {
    $blnRight = true;
}
else {
    $objSecMan = new SecurityManager(
        'file:'.$objFile->FileID, 
        $objAdminInfo->getUserID()
    );
    $blnRight = $objSecMan->processResource('view');
}

// if the user can modify and or publish, can even view the file
if(!$blnRight) {
    $blnRight = $objSecMan->processResource('modify');

    if(!$blnRight) {
        $blnRight = $objSecMan->processResource('publish');
    }       
}

//$strPath = __UPLOADS__.DIRECTORY_SEPARATOR.$objFile->FileID;
$strPath = 'subdept.csv';

if (file_exists($strPath) && $blnRight) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.$strPath);//$objFile->Filename);
    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($strPath));
    ob_clean();
    flush();
    readfile($strPath);
    exit;
}
else {
    die('Restricted access');
}   
?>

When I comment the code before $strPath it works, so it must be something in the bloody framework. I would like to throw the whole CMS away.

当我在 $strPath 之前注释代码时它可以工作,所以它必须是血腥框架中的东西。我想扔掉整个 CMS。

回答by Prabhu R

Check for transfer-encoding header. If the transfer encoding is chunked, then the Content-Length field will not be set

检查传输编码标头。如果传输编码是分块的,则不会设置 Content-Length 字段

Possible causes, is that ZendServer does not do chunked encoding, whereas Apache does

可能的原因是 ZendServer 不进行分块编码,而 Apache

See the following links for details

详情请参阅以下链接

http://en.wikipedia.org/wiki/Chunked_transfer_encoding

http://en.wikipedia.org/wiki/Chunked_transfer_encoding

http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html

http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html

回答by Gumbo

Make sure that the file really exists under the specified path (is_filechecks both the existance and if the file is a regular file) and is readable (is_readable) before sending it to the client. filesizereturns falseif an error occured. So that might be the cause of your 0 value or empty value for Content-Length.

在将文件发送到客户端之前,请确保该文件确实存在于指定路径下(is_file检查是否存在以及该文件是否为常规文件)并且是可读的 ( is_readable)。如果发生错误,filesize返回false。所以这可能是导致Content-Length为 0 值或空值的原因。

And, as Ferdinand Beyer already mentioned, make sure that your script is portable and can handle different environments.

而且,正如Ferdinand Beyer 已经提到的,确保您的脚本是可移植的并且可以处理不同的环境。

回答by bucabay

Do you get any errors if you enable errors?

如果您启用错误,您会收到任何错误吗?

error_reporting(E_ALL);
ini_set('display_errors', '1');

回答by Ferdinand Beyer

Are you sure the file exists on the production server? Maybe it's a case sensitivity issue (e.g, "File" and "file" are the same on Windows, but different on UNIX)? Does the user running Apache/PHP have read access?

您确定该文件存在于生产服务器上吗?也许这是一个区分大小写的问题(例如,“文件”和“文件”在 Windows 上相同,但在 UNIX 上不同)?运行 Apache/PHP 的用户是否具有读取权限?

回答by Paul Lammertsma

A problem may be that Apache is gzipping your download, taking care of correcting the Content-Length, or in your case, removing that header and adding

一个问题可能是 Apache 正在压缩您的下载,负责更正 Content-Length,或者在您的情况下,删除该标头并添加

Content-Encoding: chunked

内容编码:分块

You can add a .htaccessRewriteRule to disable gzip:

你可以添加一个.htaccessRewriteRule 来禁用 gzip:

RewriteRule . - [E=no-gzip:1]