牧羊岛

懒是不会有好果子吃滴//

导航

FCKeditor上传文件重命名for php

Posted on 2010-07-12 14:32  牧羊岛  阅读(1807)  评论(0编辑  收藏  举报

版本  2.6.4.1 Build 23187

 

FCKeditor上传文件是不会重命名的,除非是有崇明文件存在。例如:当上传第二个“0104_p5.jpg”图片时,FCKeditor就会将新文件重命名为“0104_p5(1).jpg”,可目前项目里不需要这样,需要统一的(如“20100712061546_7199f4.jpg”)这样日期加6位随机的命名规则。于是,就有了如下改动:

首先,找到io.php的SanitizeFileName函数(约在206行~270行之间,我的在266行),原函数为:

// Do a cleanup of the file name to avoid possible problems
function SanitizeFileName( $sNewFileName )
{
global $Config ;
$sNewFileName = stripslashes( $sNewFileName ) ;
// Replace dots in the name with underscores (only one dot can be there... security issue).
if ( $Config['ForceSingleExtension'] )
$sNewFileName = preg_replace( '/\\.(?![^.]*$)/', '_', $sNewFileName ) ;
// Remove \ / | : ? * " < >
$sNewFileName = preg_replace( '/\\\\|\\/|\\||\\:|\\?|\\*|"|<|>|[[:cntrl:]]/', '_', $sNewFileName ) ;
return $sNewFileName ;
}

这个函数用来修正文件名的字符,就拿他开刀,改为:

// Do a cleanup of the file name to avoid possible problems
function SanitizeFileName( $sNewFileName )
{
global $Config ;
$sNewFileName = stripslashes( $sNewFileName ) ;
// Replace dots in the name with underscores (only one dot can be there... security issue).
if ( $Config['ForceSingleExtension'] )
$sNewFileName = preg_replace( '/\\.(?![^.]*$)/', '_', $sNewFileName ) ;
// Remove \ / | : ? * " < >
//$sNewFileName = preg_replace( '/\\\\|\\/|\\||\\:|\\?|\\*|"|<|>|[[:cntrl:]]/', '_', $sNewFileName ) ;

//注释上面一行原有的文件名,并在下面自定义文件命名规则

$sExtension = substr( $sNewFileName, ( strrpos($sNewFileName, '.') + 1 ) ) ;
$sNewFileName = my_setfilename().'.'.$sExtension;
//修改结束

return $sNewFileName ;
}

 

这里使用了一个新的命名规则函数“my_setfilename”,内容如下:

//自定义文件命名规则函数
function my_setfilename(){
//生成随机字符串
$string = 'abcdefghijklmnopgrstuvwxyz0123456789';
$rand = '';
for ($x=0;$x<6;$x++)
$rand .= substr($string,mt_rand(0,strlen($string)-1),1);
return date("YmdHis").'_'.$rand;//以“日期_随机字符串”方式返回新文件名
}

 

补充:按照日期分类文件夹存放文件,我没有用

方法:找到config.php文件,32行的样子:

// Path to user files relative to the document root.
$Config['UserFilesPath'] = '/Upload/' ;
改为:

// Path to user files relative to the document root.
$Config['UserFilesPath'] = '/Upload/' .date("Ymd").'/';
//当文件夹不存在的时候貌似会自动创建

 

结束!