zoukankan      html  css  js  c++  java
  • FCKeditor上传文件重命名for php

    版本  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").'/';
    //当文件夹不存在的时候貌似会自动创建

    结束!

  • 相关阅读:
    oracle plsql 统计
    oracle plsql 自定义异常
    oracle plsql 异常
    oracle 游标
    oracle 存储函数,更新库存
    oracle TRUNC()函数
    plsql 的三种循环
    plsql if
    plsql 记录型变量
    CAS示例环境部署及配置
  • 原文地址:https://www.cnblogs.com/ShepherdIsland/p/FCKeditor_Rename_PHP.html
Copyright © 2011-2022 走看看