[MOD]子目录存放附件 - 按月份或日期

在这里讨论PHBB3.0.X MOD, 以及和其他系统的整合
发表回复
头像
IOsetting
论坛管理员
论坛管理员
帖子: 3628
注册: 2006-10-17 1:48

[MOD]子目录存放附件 - 按月份或日期

帖子 IOsetting » 2008-07-09 15:59

#######################################################
## 名称: 子目录存放附件 - 按月份或日期
## 作者: IOsetting
## 功能描述:
## 1. 按日期以子目录存放附件
## 2(可选). 按 <用户ID>_真实文件名_<5位随机值>.真实扩展名 的格式存储附件
## 3(可选). 所有附件链接均为真实文件地址. 对于盗链, 请使用.httaccess来控制
##
##
## 注意: 在已经运行的论坛上安装此MOD, 如果有附件已经存在而使用功能2和3 会导致下载过去的附件时出现随机字符串文件名, 此问题请用本文最后附带的脚本解决.
##
## 版本: Build 20080819
##
## 安装难度: Easy
## 估计时间: 10 Minutes
##
##
#######################################################



打开
common.php
找到

代码: 全选

require($phpbb_root_path . 'includes/functions_content.' . $phpEx);
后面添加

代码: 全选

require($phpbb_root_path . 'includes/functions_extra.' . $phpEx);


打开
download/file.php
找到

代码: 全选

if (!$attachment)
{
	trigger_error('ERROR_NO_ATTACHMENT');
}

$attachment['physical_filename'] = basename($attachment['physical_filename']);
替换为

代码: 全选

if (!$attachment)
{
	trigger_error('ERROR_NO_ATTACHMENT');
}

$attachment['physical_foldername'] = dirname($attachment['physical_filename']);
$attachment['physical_filename'] = basename($attachment['physical_filename']);
找到

代码: 全选

		redirect($phpbb_root_path . $config['upload_path'] . '/' . $attachment['physical_filename']);
替换为

代码: 全选

		redirect($phpbb_root_path . $config['upload_path'] . '/' . $attachment['physical_foldername'] . '/' . $attachment['physical_filename']);
找到

代码: 全选

	$filename = $phpbb_root_path . $upload_dir . '/' . $attachment['physical_filename'];
替换为

代码: 全选

	$filename = $phpbb_root_path . $upload_dir . '/' . $attachment['physical_foldername'] . '/' . $attachment['physical_filename'];


打开
includes/functions_admin.php
找到

代码: 全选

		WHERE physical_filename = '" . $db->sql_escape(basename($filename)) . "'";
替换为

代码: 全选

		WHERE physical_filename = '" . $db->sql_escape($filename) . "'";
找到

代码: 全选

 	{
 		return false;
 	}

	$filename = ($mode == 'thumbnail') ? 'thumb_' . basename($filename) : basename($filename);

代码: 全选

	$filename = ($mode == 'thumbnail') ? 'thumb_' . basename($filename) : basename($filename);
替换为

代码: 全选

	if ($mode == 'thumbnail') {
		$filename_name = basename($filename);
		$filename_path = dirname($filename);
		$filename = $filename_path . '/thumb_' . $filename_name;
	}
	//$filename = ($mode == 'thumbnail') ? 'thumb_' . basename($filename) : basename($filename);

打开
includes/functions_content.php

找到

代码: 全选

		$filename = $phpbb_root_path . $config['upload_path'] . '/' . basename($attachment['physical_filename']);
		$thumbnail_filename = $phpbb_root_path . $config['upload_path'] . '/thumb_' . basename($attachment['physical_filename']);
替换为

代码: 全选

		$filename = $phpbb_root_path . $config['upload_path'] . '/' . dirname($attachment['physical_filename']) . '/' . basename($attachment['physical_filename']);
		$thumbnail_filename = $phpbb_root_path . $config['upload_path'] . '/' . dirname($attachment['physical_filename']) . '/thumb_' . basename($attachment['physical_filename']);
找到

代码: 全选

			$download_link = append_sid("{$phpbb_root_path}download/file.$phpEx", 'id=' . $attachment['attach_id']);
<如果不打算使用功能3, 请跳过此步> 替换为

代码: 全选

			$download_link = $phpbb_root_path . $config['upload_path'] . '/' . $attachment['physical_filename'];

找到

代码: 全选

					$inline_link = append_sid("{$phpbb_root_path}download/file.$phpEx", 'id=' . $attachment['attach_id']);
					$download_link .= '&mode=view';
<如果不打算使用功能3, 请跳过此步> 替换为

代码: 全选

					//$inline_link = append_sid("{$phpbb_root_path}download/file.$phpEx", 'id=' . $attachment['attach_id']);
					$inline_link = $download_link;
					//$download_link .= '&mode=view';

找到

代码: 全选

					$thumbnail_link = append_sid("{$phpbb_root_path}download/file.$phpEx", 'id=' . $attachment['attach_id'] . '&t=1');
					$download_link .= '&mode=view';
<如果不打算使用功能3, 请跳过此步> 替换为

代码: 全选

					//$thumbnail_link = append_sid("{$phpbb_root_path}download/file.$phpEx", 'id=' . $attachment['attach_id'] . '&t=1');
					$thumbnail_link = $phpbb_root_path . $config['upload_path'] . '/' . dirname($attachment['physical_filename']) . '/thumb_' . basename($attachment['physical_filename']);
					//$download_link .= '&mode=view';


新增
includes/functions_extra.php

代码: 全选

<?php
/**
*
* @package phpBB3
* @version $Id: functions_extra.php,v 1.000 2008/05/10 15:07:35 mikov chain Exp $
* @copyright (c) 2007 phpBB China Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

// Extra global functions

/**
* set_var
*
* Set variable, used by {@link request_var the request_var function}
*
* @access private	012345678
*/
function user_path_gen($user_id)
{
	global $auth, $user, $config, $db, $cache;
	global $phpbb_root_path, $phpEx;
	
	if (preg_match('#^\d+#i', $user_id)){
		$length = strlen($user_id);
		if ($length < 6){
			$user_id = sprintf("%06d", $user_id);
			$length = 6;
		}
		$user_path = $config['upload_path'].'/'.substr($user_id, 0, $length-4);
		$user_path .= '/'.substr($user_id, $length-4, 2);
		$user_path .= '/'.substr($user_id, $length-2, 2);
		return $user_path;
	} else {
		return $config['upload_path'].'/unknown';
	}
}

function date_path_gen()
{
	$attach_subdir = 'm_'.date('Ym');
	//$attach_subdir = 'd_'.date('Ymd');
	return $attach_subdir;
}

 function mkdeepdir($path)
{
    unset($subpath);
    $path_array = explode('/', $path);
    foreach($path_array AS $path_unit)
    {
        if($path_unit=='') continue;
        $subpath_array[] = $path_unit;
        $subpath = implode('/', $subpath_array);
        if(!is_dir($subpath))
        {
            @mkdir($subpath, 0777);
        }
    }
}

?>
注意: 可以自行调整目录名, 请修改其中用于控制子目录名的函数

代码: 全选

function date_path_gen()
{
	$attach_subdir = 'm_'.date('Ym'); // -- 显示为 m_200807 或者 m_200812  这样
	//$attach_subdir = 'd_'.date('Ymd'); // -- 显示为 d_20080709 或者 d_20081231 这样
	return $attach_subdir;
}

打开
includes/functions_posting.php

找到

代码: 全选

	$file->clean_filename('unique', $user->data['user_id'] . '_');
<如果不打算使用功能2, 请跳过此步> 替换为

代码: 全选

	$file->clean_filename('real', $user->data['user_id'] . '_');

找到

代码: 全选

	$file->move_file($config['upload_path'], false, $no_image);
替换为

代码: 全选

	$sub_path = date_path_gen();
	$file->move_file($config['upload_path'] . '/' . $sub_path, false, $no_image);

找到

代码: 全选

	$filedata['physical_filename'] = $file->get('realname');
替换为

代码: 全选

	$filedata['physical_filename'] = $sub_path. '/' . $file->get('realname');

找到

代码: 全选

				if (!@file_exists($phpbb_root_path . $config['upload_path'] . '/' . basename($orphan_rows[$attach_row['attach_id']]['physical_filename'])))
替换为

代码: 全选

				if (!@file_exists($phpbb_root_path . $config['upload_path'] . '/' . $orphan_rows[$attach_row['attach_id']]['physical_filename']))


打开
includes/functions_privmsgs.php

找到

代码: 全选

				if (!@file_exists($phpbb_root_path . $config['upload_path'] . '/' . basename($orphan_rows[$attach_row['attach_id']]['physical_filename'])))
替换为

代码: 全选

				if (!@file_exists($phpbb_root_path . $config['upload_path'] . '/' . $orphan_rows[$attach_row['attach_id']]['physical_filename']))


打开
includes/functions_upload.php
找到

代码: 全选

 				{
 					$this->realname = substr($this->realname, 0, strpos($this->realname, '.'));
 				}
<如果不打算使用功能2, 请跳过此步> 后面添加

代码: 全选

				$this->realname = $this->realname . '_' . substr(md5(unique_id()),0,5);

找到

代码: 全选

 		// Check if the destination path exist...
后面添加

代码: 全选

		if(!is_dir($this->destination_path)) {
			mkdeepdir($this->destination_path);
			@fclose(fopen($this->destination_path.'/index.html', 'w'));
		}

转换标准版phpBB3的附件文件名的脚本, 将附件重命名为保留原文件名信息并带扩展名的形式, 同时更新数据库中的记录.

将下面的代码另存为convert_attachment.php, 置于论坛根目录下用浏览器访问来执行. 在执行前请一定备份你的论坛附件文件夹和数据库中 phpbb_attachments 表的内容
其中 $interval = 10 用来控制每步转换的附件数, 视你的机器速度而定.

代码: 全选

<?php
/**
*
* @version $Id: convert_attchment.php 2008-08-19 17:11:26Z IOsetting $
* @copyright (c) 2005 phpbbchina.com
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'includes/functions_display.' . $phpEx);

$scriptname = 'convert_attachment.php';
$interval = 10;

$page	= request_var('p', 0);

$sql = 'SELECT COUNT(attach_id) AS num_attach
	FROM ' . ATTACHMENTS_TABLE . ' 
	ORDER BY attach_id ASC';
$result = $db->sql_query($sql);
$attachs_count = (int) $db->sql_fetchfield('num_attach');
$db->sql_freeresult($result);

$start = $interval * $page;

if (($attachs_count - $start) > $interval){
	$complete = FALSE;
	$step = $interval;
} else {
	$complete = TRUE;
	$step = $attachs_count - $start;
}

$sql = 'SELECT attach_id, poster_id, physical_filename, real_filename, extension, thumbnail
	FROM ' . ATTACHMENTS_TABLE . ' 
	ORDER BY attach_id ASC LIMIT ' . $start . ', ' . $step;
$result = $db->sql_query($sql);

echo dheader();

while ($row = $db->sql_fetchrow($result))
{
	$destination_file = substr($row['real_filename'], 0, strpos($row['real_filename'], '.'));
	$destination_file = $row['poster_id'] . '_' . $destination_file . '_' . substr(md5(unique_id()),0,5) . '.' . $row['extension'];
	
	$destination = $phpbb_root_path . $config['upload_path'] . '/' . $destination_file;
	$source = $phpbb_root_path . $config['upload_path'] . '/' . $row['physical_filename'];
	
	if (!@rename($source, $destination))	{
		echo sprintf("Rename error from %s to %s ", $source, $destination);
	}
	
	if ($row['thumbnail'] == 1) {
		$destination = $phpbb_root_path . $config['upload_path'] . '/thumb_' . $destination_file;
		$source = $phpbb_root_path . $config['upload_path'] . '/thumb_' . $row['physical_filename'];
		
		if (!@rename($source, $destination))	{
			echo sprintf("Thumb rename error from %s to %s ", $source, $destination);
		}
	}

	$update_sql = 'UPDATE ' . ATTACHMENTS_TABLE . ' SET 
		physical_filename = \'' . $destination_file . '\'
		WHERE attach_id = ' . $row['attach_id'];
	$db->sql_query($update_sql);
	
	echo sprintf("Successfully convert attachment %s <br />From %s to %s<br />", $row['attach_id'], $row['physical_filename'], $destination_file);
}
$db->sql_freeresult($result);

echo dfooter();

function dheader() {

	global $start, $attachs_count, $step;
	return "
<html>
<head>
<title>Attachment No. " . ($start+1) . " to " . ($start+$step) . ", all $attachs_count - attachment convert</title>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf8\">
<style>
A:visited	{COLOR: #3A4273; TEXT-DECORATION: none}
A:link		{COLOR: #3A4273; TEXT-DECORATION: none}
A:hover		{COLOR: #3A4273; TEXT-DECORATION: underline}
body,table,td	{COLOR: #3A4273; FONT-FAMILY: Tahoma, Verdana, Arial; FONT-SIZE: 12px; LINE-HEIGHT: 20px; scrollbar-base-color: #E3E3EA; scrollbar-arrow-color: #5C5C8D}
input		{COLOR: #085878; FONT-FAMILY: Tahoma, Verdana, Arial; FONT-SIZE: 12px; background-color: #3A4273; color: #FFFFFF; scrollbar-base-color: #E3E3EA; scrollbar-arrow-color: #5C5C8D}
.install	{FONT-FAMILY: Arial, Verdana; FONT-SIZE: 20px; FONT-WEIGHT: bold; COLOR: #000000}
</style>
</head>
<body bgcolor=\"#3A4273\" text=\"#000000\">
<table width=\"95%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" bgcolor=\"#FFFFFF\" align=\"center\">
  <tr>
    <td>
      <table width=\"98%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\">
        <tr>
          <td>
          Attachment No. " . ($start+1) . " to " . ($start+$step) . ", all $attachs_count - attachment convert<br /><br />
	";
}

function dfooter() {
	global $scriptname, $complete, $page;
	
	if (!$complete) {
		$next_step_link = "<a href=\"".$scriptname."?p=". ($page + 1) ."\">Next step</a>";
	} else {
		$next_step_link = "<b>Completed</b>";
	}
	
	return "
			<br />$next_step_link
          </td>
        </tr>
        <tr>
          <td>
            <hr noshade align=\"center\" width=\"100%\" size=\"1\">
          </td>
        </tr>
        <tr>
          <td align=\"center\">
            <b style=\"font-size: 11px\">Powered by <a href=\"http://www.phpbbchina.com\" target=\"_blank\">phpBBChina</a> , &nbsp; Copyright &copy; <a href=\"http://www.phpbbchina.com\" target=\"_blank\">www.phpBBChina.com</a>, 2006-2008</b>
          </td>
        </tr>
      </table>
    </td>
  </tr>
</table>
<br>
</body>
</html>
	";
}
?>

叶知秋
学有小成
学有小成
帖子: 111
注册: 2007-07-29 1:41

Re: [MOD]子目录存放附件 - 按月份或日期

帖子 叶知秋 » 2008-08-15 4:10

:grin: :grin: :grin: 附件最实用的mod

头像
zj0789
初来乍到
初来乍到
帖子: 8
注册: 2008-08-25 22:01

Re: [MOD]子目录存放附件 - 按月份或日期

帖子 zj0789 » 2008-08-26 9:57

嗯,dz后台就有这种类似的选项。
一直很喜欢这个功能,用起来管理附件非常方便
谢谢楼主~! :love:

头像
IOsetting
论坛管理员
论坛管理员
帖子: 3628
注册: 2006-10-17 1:48

[MOD]子目录存放附件 - 按月份或日期

帖子 IOsetting » 2018-12-05 2:03

https://www.phpbb.com/community/viewtop ... #p13504286

修改中需要增加对 includes/mcp/mcp_main.php的修改, 否则会影响在版主控制台中复制主题
找到

代码: 全选

'physical_filename'	=> (string) utf8_basename($attach_row['physical_filename']),
替换为

代码: 全选

'physical_filename'	=> (string) ($attach_row['physical_filename']),

发表回复