[英]Content-Length header always zero
我通過以下方式設置標題:
header('Content-Length: ' . filesize($strPath));
在裝有ZendServer的PC上,它可以正常工作,並且我可以下載具有正確文件大小的文件。 在生產服務器(帶有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。
檢查傳輸編碼標頭。 如果傳輸編碼是分塊的,則不會設置Content-Length字段
可能的原因是ZendServer不會執行分塊編碼,而Apache會進行分塊編碼
有關詳細信息,請參見以下鏈接
在將文件發送到客戶端之前,請確保該文件確實存在於指定路徑下( is_file
既檢查是否存在,又檢查該文件是否為常規文件)並且可讀( is_readable
可讀)。 如果發生錯誤, filesize
返回false 。 因此,這可能是導致Content-Length的值為0或值為空的原因。
並且,如Ferdinand Beyer所述 ,請確保您的腳本可移植並且可以處理不同的環境。
您確定文件在生產服務器上存在嗎? 也許是區分大小寫的問題(例如,“文件”和“文件”在Windows上相同,但在UNIX上不同)? 運行Apache / PHP的用戶是否具有讀取權限?
如果啟用錯誤,會收到任何錯誤消息嗎?
error_reporting(E_ALL);
ini_set('display_errors', '1');
問題可能是Apache正在壓縮您的下載文件,以糾正Content-Length,或者在您的情況下,刪除該標頭並添加
內容編碼:分塊
您可以添加.htaccess
RewriteRule以禁用gzip:
RewriteRule . - [E=no-gzip:1]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.