fckeditor 上传图片 php_FCKeditor上传文件重命名for php

本文介绍如何在FCKeditor 2.6.4.1中修改SanitizeFileName函数,实现日期加随机数的统一文件命名,并调整文件夹结构以便按日期分类。通过自定义函数my_setfilename生成文件名,提升上传文件管理的灵活性。
摘要由CSDN通过智能技术生成

FCKeditor 版本 2.6.4.1Build 23187

468c1822757f1197d375376ef7d46389.png

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").'/';

//当文件夹不存在的时候貌似会自动创建

本文来自投稿,不代表访得立场,如若转载,请注明出处:http://www.found5.com//view/159.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值