xiuno下载附件的压缩文件,下载成功后,压缩包是坏的。但是在服务器上的是正常的。重装了网站程序,问题一样。在二级目录建立的xiuno站点下载的文件都正常。假如配置问题,二级目录的xiuno站是正常的。假如程序问题,重装也是一样问题。
网上真没有解决方法,花了好长时间折腾才找到解决方法,想不通为什么会这样,明明以前使用没什么问题的,差点放弃了。
解决方法
修改route/attach.php
} elseif($action == 'download') {
// hook attach_download_start.php
// 判断权限
$aid = param(2, 0);
$attach = attach_read($aid);
empty($attach) AND message(-1, lang('attach_not_exists'));
$tid = $attach['tid'];
$thread = thread_read($tid);
$fid = $thread['fid'];
$allowdown = forum_access_user($fid, $gid, 'allowdown');
empty($allowdown) AND message(-1, lang('insufficient_privilege_to_download'));
$attachpath = $conf['upload_path'].'attach/'.$attach['filename'];
$attachurl = $conf['upload_url'].'attach/'.$attach['filename'];
!is_file($attachpath) AND message(-1, lang('attach_not_exists'));
// 关键修复:禁用所有输出压缩
if (function_exists('apache_setenv')) {
@apache_setenv('no-gzip', 1);
}
@ini_set('zlib.output_compression', 0);
@ini_set('output_buffering', 0);
// 清空所有输出缓冲
while (ob_get_level()) {
ob_end_clean();
}
$type = 'php';
// hook attach_output_before.php
// php 输出
if($type == 'php') {
attach_update($aid, array('downloads+'=>1));
$filesize = $attach['filesize'];
if(stripos($_SERVER["HTTP_USER_AGENT"], 'MSIE') !== FALSE || stripos($_SERVER["HTTP_USER_AGENT"], 'Edge') !== FALSE || stripos($_SERVER["HTTP_USER_AGENT"], 'Trident') !== FALSE) {
$attach['orgfilename'] = urlencode($attach['orgfilename']);
$attach['orgfilename'] = str_replace("+", "%20", $attach['orgfilename']);
}
$timefmt = date('D, d M Y H:i:s', $time).' GMT';
header('Date: '.$timefmt);
header('Last-Modified: '.$timefmt);
header('Expires: '.$timefmt);
// 修改缓存策略:禁用缓存
header('Cache-Control: no-cache, no-store, must-revalidate');
header('Pragma: no-cache');
header('Content-Transfer-Encoding: binary');
header("Pragma: public");
header('Content-Disposition: attachment; filename="'.$attach['orgfilename'].'"');
header('Content-Type: application/octet-stream');
// 关键:确保不压缩
header('Content-Encoding: identity');
// hook attach_download_readfile_before.php
// 使用 fpassthru 替代 readfile,更稳定
$fp = fopen($attachpath, 'rb');
if ($fp) {
fpassthru($fp);
fclose($fp);
} else {
readfile($attachpath);
}
exit;
} else {
// hook attach_download_location_before.php
http_location($attachurl);
}
}
如果以上方法没解决就在修改下配置文件
location ~* \.(zip|rar|7z)$ {
gzip off;
}
提示
因为此问题,一直没人反馈,并不知道什么时候出现的问题,所以下载附件的同志们,打不开请重新下载。