Content-Length标头始终为零

时间:2009-08-26 12:53:39

标签: php apache header content-length

我按以下方式设置标题:

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

在我的电脑上使用ZendServer它工作正常,我可以下载一个文件大小正确的文件。在生产服务器上,使用Apache和编译PHP的Solaris,我得到一个文件大小等于零的文件,因此是一个空文件。

是否有配置参数?可以阻止设置'Content-Length:1222'的东西?

感谢。

代码:

<?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');
}   
?>

当我在$ strPath之前评论代码时,它起作用,所以它必须是血腥框架中的东西。我想把整个CMS扔掉。

5 个答案:

答案 0 :(得分:4)

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

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

有关详细信息,请参阅以下链接

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

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

答案 1 :(得分:2)

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

并且,作为Ferdinand Beyer already mentioned,确保您的脚本是可移植的,并且可以处理不同的环境。

答案 2 :(得分:0)

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

答案 3 :(得分:0)

如果启用错误,是否会出现任何错误?

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

答案 4 :(得分:0)

问题可能是Apache正在下载您的内容,负责纠正内容长度,或者在您的情况下,删除该标题并添加

  

Content-Encoding:chunked

您可以添加.htaccess RewriteRule来禁用gzip:

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