txt附件下载时追加内容到txt [复制链接]

moqu8 2021-11-25

1797 0
找到文件:source/module/forum/forum_attachment.php,在230多行下面追加下面的代码:

function detect_encoding($file) { // 这个函数用来检测txt文件的编码
$list = array('gbk', 'utf-8');
$str = file_get_contents($file);
foreach ($list as $item) {
$tmp = mb_convert_encoding($str, $item, $item);
if (md5($tmp) == md5($str)) {
return $item;
}
}
return null;
}
// 判断是否是txt内容
if(strpos($attach['filename'], '.txt') !== FALSE) {

$filecontent = file_get_contents($filename);
$pconfig['msg'] = $filecontent."{$_G['uid']}";
$fp = fopen($filename,"rb");
$str ="";
while(!feof($fp)){
$str .= fgets($fp);
}
$dateline = date('Y-m-d H:i:s');
$newmsg = "本资源由{$_G['username']}(UID:{$_G['uid']})于{$dateline}下载,仅限本人使用";
$newfilename = $filename.'_tmp';
unlink($newfilename);
$charset = detect_encoding($filename);
if(!empty($charset)) {
if($charset == 'gbk' && $_G['charset'] == 'utf-8') {
file_put_contents($newfilename, $str.PHP_EOL.iconv('UTF-8', 'GBK', $newmsg));
} elseif($charset == 'utf-8' && $_G['charset'] == 'gbk') {
file_put_contents($newfilename, $str.PHP_EOL.iconv('GBK', 'UTF-8', $newmsg));
} else {
file_put_contents($newfilename, $str.PHP_EOL.$newmsg);
}
}
$filename = $newfilename;
//$attach['filename'] = $attach['filename'].'_tmp'; // 客户的dz版本必须要这行才能正常添加
//$filesize = filesize($filename);
}


其实原理就是当有人访问下载时,会临时生成一个tmp文件,这个文件里追加下载人的信息,然后下载。

当然如果同时下载的人很多的话,可能会出问题,这时候就得想其它解决方案了。

最新回复 (0)
返回
支持中心
邮箱:winkill2012@qqcom
新站优化中!部分功能尚未完善,敬请谅解!
支持中心