## 名称: 子目录存放附件 - 按月份或日期
## 作者: IOsetting
## 功能描述:
## 1. 按日期以子目录存放附件
## 2(可选). 按 <用户ID>_真实文件名_<5位随机值>.真实扩展名 的格式存储附件
## 3(可选). 所有附件链接均为真实文件地址. 对于盗链, 请使用.httaccess来控制
##
##
## 注意: 在已经运行的论坛上安装此MOD, 如果有附件已经存在而使用功能2和3 会导致下载过去的附件时出现随机字符串文件名, 此问题请用本文最后附带的脚本解决.
##
## 版本: Build 20080819
##
## 安装难度: Easy
## 估计时间: 10 Minutes
##
##
#######################################################
打开
common.php
找到
Code: Select all
require($phpbb_root_path . 'includes/functions_content.' . $phpEx);
Code: Select all
require($phpbb_root_path . 'includes/functions_extra.' . $phpEx);
打开
download/file.php
找到
Code: Select all
if (!$attachment)
{
trigger_error('ERROR_NO_ATTACHMENT');
}
$attachment['physical_filename'] = basename($attachment['physical_filename']);
Code: Select all
if (!$attachment)
{
trigger_error('ERROR_NO_ATTACHMENT');
}
$attachment['physical_foldername'] = dirname($attachment['physical_filename']);
$attachment['physical_filename'] = basename($attachment['physical_filename']);
Code: Select all
redirect($phpbb_root_path . $config['upload_path'] . '/' . $attachment['physical_filename']);
Code: Select all
redirect($phpbb_root_path . $config['upload_path'] . '/' . $attachment['physical_foldername'] . '/' . $attachment['physical_filename']);
Code: Select all
$filename = $phpbb_root_path . $upload_dir . '/' . $attachment['physical_filename'];
Code: Select all
$filename = $phpbb_root_path . $upload_dir . '/' . $attachment['physical_foldername'] . '/' . $attachment['physical_filename'];
打开
includes/functions_admin.php
找到
Code: Select all
WHERE physical_filename = '" . $db->sql_escape(basename($filename)) . "'";
Code: Select all
WHERE physical_filename = '" . $db->sql_escape($filename) . "'";
Code: Select all
{
return false;
}
$filename = ($mode == 'thumbnail') ? 'thumb_' . basename($filename) : basename($filename);
Code: Select all
$filename = ($mode == 'thumbnail') ? 'thumb_' . basename($filename) : basename($filename);
Code: Select all
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
找到
Code: Select all
$filename = $phpbb_root_path . $config['upload_path'] . '/' . basename($attachment['physical_filename']);
$thumbnail_filename = $phpbb_root_path . $config['upload_path'] . '/thumb_' . basename($attachment['physical_filename']);
Code: Select all
$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']);
Code: Select all
$download_link = append_sid("{$phpbb_root_path}download/file.$phpEx", 'id=' . $attachment['attach_id']);
Code: Select all
$download_link = $phpbb_root_path . $config['upload_path'] . '/' . $attachment['physical_filename'];
找到
Code: Select all
$inline_link = append_sid("{$phpbb_root_path}download/file.$phpEx", 'id=' . $attachment['attach_id']);
$download_link .= '&mode=view';
Code: Select all
//$inline_link = append_sid("{$phpbb_root_path}download/file.$phpEx", 'id=' . $attachment['attach_id']);
$inline_link = $download_link;
//$download_link .= '&mode=view';
找到
Code: Select all
$thumbnail_link = append_sid("{$phpbb_root_path}download/file.$phpEx", 'id=' . $attachment['attach_id'] . '&t=1');
$download_link .= '&mode=view';
Code: Select all
//$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
Code: Select all
<?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);
}
}
}
?>
Code: Select all
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
找到
Code: Select all
$file->clean_filename('unique', $user->data['user_id'] . '_');
Code: Select all
$file->clean_filename('real', $user->data['user_id'] . '_');
找到
Code: Select all
$file->move_file($config['upload_path'], false, $no_image);
Code: Select all
$sub_path = date_path_gen();
$file->move_file($config['upload_path'] . '/' . $sub_path, false, $no_image);
找到
Code: Select all
$filedata['physical_filename'] = $file->get('realname');
Code: Select all
$filedata['physical_filename'] = $sub_path. '/' . $file->get('realname');
找到
Code: Select all
if (!@file_exists($phpbb_root_path . $config['upload_path'] . '/' . basename($orphan_rows[$attach_row['attach_id']]['physical_filename'])))
Code: Select all
if (!@file_exists($phpbb_root_path . $config['upload_path'] . '/' . $orphan_rows[$attach_row['attach_id']]['physical_filename']))
打开
includes/functions_privmsgs.php
找到
Code: Select all
if (!@file_exists($phpbb_root_path . $config['upload_path'] . '/' . basename($orphan_rows[$attach_row['attach_id']]['physical_filename'])))
Code: Select all
if (!@file_exists($phpbb_root_path . $config['upload_path'] . '/' . $orphan_rows[$attach_row['attach_id']]['physical_filename']))
打开
includes/functions_upload.php
找到
Code: Select all
{
$this->realname = substr($this->realname, 0, strpos($this->realname, '.'));
}
Code: Select all
$this->realname = $this->realname . '_' . substr(md5(unique_id()),0,5);
找到
Code: Select all
// Check if the destination path exist...
Code: Select all
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 用来控制每步转换的附件数, 视你的机器速度而定.
Code: Select all
<?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> , Copyright © <a href=\"http://www.phpbbchina.com\" target=\"_blank\">www.phpBBChina.com</a>, 2006-2008</b>
</td>
</tr>
</table>
</td>
</tr>
</table>
<br>
</body>
</html>
";
}
?>