清泉_月逍 發表於 2024-9-8 09:38:59

Discuz! X3.5 不用插件实现上传下载附件大于4G

因最近内网服务器之间下载各种升级包需要搭建一个网站或者文件服务器,上传要能审核,下载能够查看记录,同事开始建议使用SVN、Gitlab等开源仓库,最后决定用Discuz! X3.5 搭建一个下载论坛,可以方便的隐藏不需要的菜单、或者其他模块,发帖上传审核、下载记录使用插件实现。<br />配置都已修改正常:<br />1)用户组、管理组中附件相关都已设成0,即不限制大小。(后面我改成了20971520,即小于20480M)<br /><br />2)httpd.conf中已增加(表示允许请求20G):LimitRequestBody&nbsp;&nbsp;21474836480<br />3)php.ini(独立内网,时间都设为了0):<br />upload_max_filesize = 20480M<br />memory_limit = 20480M<br />post_max_size = 20480M<br /><br />max_input_time = 0<br />max_execution_time = 0<br />但是在上传下载大附件时遇到了问题:<br /><br /><br />1、不能上传大于4G的附件,即使设成0也一样,是因为相关数据库表中<font color="#4d4d4d"><font face="Arial, sans-serif"><font style="font-size:10.5pt">maxattachsize、filesize是int类型,无符号int最大在4G的样子,我用的</font></font></font><font face="Arial, sans-serif"><font color="#4d4d4d">navicat【右键--》设计表】,修改以下表,并保存:</font></font><br /><br /><br /><font color="#4D4D4D"><font face="&amp;quot;">1)</font></font><font color="#333333"><font face="宋体"><font style="font-size:11.0pt">、</font></font></font><font color="#4D4D4D"><font face="&amp;quot;">usergroups_field-&gt;maxattachsize</font></font><font color="#4D4D4D"><font face="宋体">、</font></font><font color="#4D4D4D"><font face="&amp;quot;">maxsizeperday </font></font><font style="color:rgb(77, 77, 77)"><font face="宋体">为</font></font><font style="color:rgb(77, 77, 77)"><font face="Arial, sans-serif">bigint,长度20。长度只是显示位数,不影响最大值!</font></font><font color="#4D4D4D"><font face="&amp;quot;">2)</font></font><font color="#4D4D4D"><font face="宋体">、</font></font><font color="#4D4D4D"><font face="&amp;quot;">attachment</font></font><font color="#4D4D4D"><font face="宋体">相关表中的</font></font><font color="#4D4D4D"><font face="&amp;quot;">filesize</font></font><font color="#4D4D4D"><font face="宋体">、</font></font><font color="#4D4D4D"><font face="&amp;quot;">maxsize </font></font><font color="#4D4D4D"><font face="宋体">为</font></font><font color="#4D4D4D"><font face="&amp;quot;">bigint(20位)</font></font><font color="#4D4D4D"><font face="&amp;quot;">3)</font></font><font color="#4D4D4D"><font face="宋体">、</font></font><font color="#4D4D4D"><font face="&amp;quot;">f</font></font><font color="#4D4D4D"><font face="&amp;quot;">orum_attachtype-&gt;maxsize </font></font><font style="color:rgb(77, 77, 77)"><font face="宋体">为</font></font><font style="color:rgb(77, 77, 77)"><font face="Arial, sans-serif">bigint(20位)</font></font><font color="#4D4D4D"><font face="&amp;quot;">4)</font></font><font color="#4D4D4D"><font face="宋体">、</font></font><font color="#4D4D4D"><font face="&amp;quot;">member_count-&gt;attachsize</font></font><font color="#4D4D4D"><font face="宋体">、</font></font><font color="#4D4D4D"><font face="&amp;quot;">todayattachsize </font></font><font style="color:rgb(77, 77, 77)"><font face="宋体">为</font></font><font style="color:rgb(77, 77, 77)"><font face="Arial, sans-serif">bigint(20位)</font></font><font color="#4D4D4D"><font face="&amp;quot;">5)</font></font><font color="#4D4D4D"><font face="宋体">、</font></font><font color="#4D4D4D"><font face="&amp;quot;">forum_polloption_image-&gt;filesize&nbsp; &nbsp;</font></font><font color="#4D4D4D"><font face="宋体">【图片】</font></font><font style="color:rgb(77, 77, 77)"><font face="宋体">为</font></font><font style="color:rgb(77, 77, 77)"><font face="Arial, sans-serif">bigint(20位)</font></font><font style="color:rgb(77, 77, 77)"><font face="Arial, sans-serif">修改后重启apache,进行上传大于4G附件,可以看到</font></font>。<img title="2.jpg" id="aimg_31769" aid="31769" src1="static/image/common/none.gif" zoom="https://www.dismall.com/data/attachment/forum/202409/08/090447ijoz3hjnjgao377h.jpg" src="https://www.dismall.com/data/attachment/forum/202409/08/090447ijoz3hjnjgao377h.jpg" class="zoom" onclick="zoom(this, this.src, 0, 0, 0)" width="341" inpost="1" onmouseover="showMenu({'ctrlid':this.id,'pos':'12'})" />看起来好像正常了,但是点击下载下来只有610M(在win10笔记本上测试的),去附件文件夹里看也确实只有610M,网上找了找没有找到解决办法,本来打算放弃采用别的办法来实现,突然想到我测试过其他版本,一直用的是php7.2,改回X3.5后没有改回php8.0,配置好php8.0后,测试4.25G、7G,一直到16G上传都正常去附件文件夹里看,文件大小也正常了!<img title="附件大于4G.jpg" id="aimg_31768" aid="31768" src1="static/image/common/none.gif" zoom="https://www.dismall.com/data/attachment/forum/202409/08/090209ftwt7btlbr37s3lq.jpg" src="https://www.dismall.com/data/attachment/forum/202409/08/090209ftwt7btlbr37s3lq.jpg" class="zoom" onclick="zoom(this, this.src, 0, 0, 0)" width="483" inpost="1" onmouseover="showMenu({'ctrlid':this.id,'pos':'12'})" /><br />但是下载却遇到了麻烦,win10机器上下载超过3G大小后,就会看到下载速度变成0,然后又会尝试下载几次,最终还是会失败。win7更惨,超过1G就是失败。网上找了下资料,说是应该内存不够,使用php下载会将数据缓冲到内存,只有另谋出路了,解决办法就是用apache的<strong><font color="red"><font face="等线"><font style="font-size:10.5pt">mod_xsendfile模块下载,不走php层,我用的apache24,</font></font></font></strong>模块下载地址:https://github.com/nmaier/mod_xsendfile<font face="Arial, sans-serif">1)</font>、将<font face="Arial, sans-serif">mod_xsendfile.so</font>放置到<font face="Arial, sans-serif">apache</font>安装目录下的<font face="Arial, sans-serif">modules</font>文件夹2)、配置Apache 配置文件 将以下内容添加到httpd.conf中# 加载 x-sendfile 模块LoadModulexsendfile_modulemodules/mod_xsendfile.so<br />XSendFile on<strong>3)编辑source\module\forum\forum_attachment.php,找到下面这行(317行),然后紧接着在下面添加两行内容,</strong>function getlocalfile($filename, $readmod =2, $range_start = 0, $range_end = 0) {&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;header('X-Sendfile:'.$filename);&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;exit();<font color="#70AD47">/*&nbsp; &nbsp;&nbsp;&nbsp;if($readmod == 1 || $readmod == 3 ||$readmod == 4) {</font><font color="#70AD47">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;if($fp = @fopen($filename, 'rb')){</font><font color="#70AD47">……………………这些都要注释掉………………………………</font><font color="#70AD47">&nbsp; &nbsp;&nbsp; &nbsp; @flush(); @ob_flush();*/</font><font color="blue">}&nbsp;&nbsp;</font><font color="blue">注意最后这个</font><font color="#00ff">}</font><font color="#00ff"> </font><font color="blue">要保留</font><font color="blue">进行下载测试,不用缓冲,直接就弹出文件保存框,win7上上传、下载测试7G,16G均正常!已经可以满足上传下载各种大包的需求,更大的就没在测试了。希望对有需要的朋友有帮助!</font><br /><br />quot

056476 發表於 2024-9-9 21:46:49

牛,虽然看不懂。但是感觉很厉害👍🏻

puning 發表於 2024-9-29 12:48:40

https://addon.dismall.com/plugins/pn_ftp_c2s.html<br />这个方案更好,不用把上传内存设置加大,更省资源,支持无限大文件

puning 發表於 2024-9-29 13:03:38

也无需改代码支持无限大文件下载
頁: [1]
查看完整版本: Discuz! X3.5 不用插件实现上传下载附件大于4G