简体   繁体   English

内容长度标头始终为零

[英]Content-Length header always zero

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. 在装有ZendServer的PC上,它可以正常工作,并且我可以下载具有正确文件大小的文件。 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. 在生产服务器(带有Apache和已编译PHP的Solaris)上,我得到一个文件,该文件的大小等于零,因此为空文件。

Is there a config parameter? 有配置参数吗? Something that can prevent to set 'Content-Length: 1222'? 可以阻止设置“ Content-Length: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. 当我在$ strPath之前注释代码时,它可以工作,因此它一定是在血腥的框架中。 I would like to throw the whole CMS away. 我想丢掉整个CMS。

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

Make sure that the file really exists under the specified path ( is_file checks both the existance and if the file is a regular file) and is readable ( is_readable ) before sending it to the client. 在将文件发送到客户端之前,请确保该文件确实存在于指定路径下( is_file既检查是否存在,又检查该文件是否为常规文件)并且可读( is_readable可读)。 filesize returns false if an error occured. 如果发生错误, filesize返回false So that might be the cause of your 0 value or empty value for Content-Length . 因此,这可能是导致Content-Length的值为0或值为空的原因。

And, as Ferdinand Beyer already mentioned , make sure that your script is portable and can handle different environments. 并且,如Ferdinand Beyer所述 ,请确保您的脚本可移植并且可以处理不同的环境。

Are you sure the file exists on the production server? 您确定文件在生产服务器上存在吗? Maybe it's a case sensitivity issue (eg, "File" and "file" are the same on Windows, but different on UNIX)? 也许是区分大小写的问题(例如,“文件”和“文件”在Windows上相同,但在UNIX上不同)? Does the user running Apache/PHP have read access? 运行Apache / PHP的用户是否具有读取权限?

Do you get any errors if you enable errors? 如果启用错误,会收到任何错误消息吗?

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

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 .htaccess RewriteRule to disable gzip: 您可以添加.htaccess RewriteRule以禁用gzip:

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

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

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