首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Content-Length报头始终为零

Content-Length报头始终为零
EN

Stack Overflow用户
提问于 2009-08-26 12:53:39
回答 5查看 11K关注 0票数 2

我用以下方式设置了一个header:

代码语言:javascript
复制
header('Content-Length: ' . filesize($strPath));

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

是否有配置参数?什么可以阻止设置'Content-Length: 1222'?

谢谢。

代码:

代码语言:javascript
复制
<?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都扔掉。

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2009-08-26 14:02:55

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

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

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

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

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

票数 5
EN

Stack Overflow用户

发布于 2009-08-26 13:03:53

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

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

票数 2
EN

Stack Overflow用户

发布于 2009-08-26 12:58:14

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1334471

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档