内容长度标头始终为零
-
20-09-2019 - |
题
我通过以下方式设置标题:
header('Content-Length: ' . filesize($strPath));
在我的装有 ZendServer 的 PC 上,它工作正常,我可以下载具有正确文件大小的文件。在生产服务器(带有 Apache 和已编译 PHP 的 Solaris)上,我得到一个文件大小为零的文件,因此是一个空文件。
有配置参数吗?可以阻止设置“内容长度:”的东西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。
解决方案
检查Transfer-Encoding头。如果传输编码分块,然后内容长度字段将不被设置
<强> 可能的原因是,ZendServer对其进行访问不会执行分块编码,而在Apache存在 强>
详见以下链接
其他提示
确认该文件是否指定路径下存在( is_file
同时检查存及其如果该文件将其发送到客户端之前是一个普通文件),并且是可读的( is_readable
)。 filesize
返回假如果发生错误。所以这可能是您的0值或空值的针对病因的的Content-Length 的。
和,作为费迪南德拜尔已经提到时,使确保你的脚本是便携式的,可以处理不同的环境。
您确定该文件存在在生产服务器上?也许这是一个区分大小写的问题(例如,“文件”和“文件”是相同的Windows版本,但不同的UNIX上的)?是否运行Apache / PHP的用户具有读取权限?
做,如果你让你的错误得到任何错误?
error_reporting(E_ALL);
ini_set('display_errors', '1');
问题可能是 Apache 正在压缩您的下载,注意更正内容长度,或者在您的情况下,删除该标头并添加
内容编码:分块的
您可以添加一个 .htaccess
RewriteRule 禁用 gzip:
RewriteRule . - [E=no-gzip:1]
不隶属于 StackOverflow