漂泊的云,停留的心!
人的头脑太复杂,时间过得久……

WordPress V3.9.2上传文件自动重命名文件名称和默认的年月保存目录的修改方法

WordPress上传文件默认是不改变文件名称的,可对中文文件名而言,某些系统、某些浏览器访问是会出现问题滴,那么怎样让Wordpress上传文件自动重命名呐?

以wordpress 3.9.0为例,打开“wp-admin/includes/file.php”文件,找到第313行和452行这段代码:

// Move the file to the uploads dir
    new_file =uploads['path'] . "/filename";   //主要是修改这行的代码!
    if ( false === @ rename(file['tmp_name'], new_file ) ) {
        if ( 0 === strpos(uploads['basedir'], ABSPATH ) )
            error_path = str_replace( ABSPATH, '',uploads['basedir'] ) . uploads['subdir'];
        elseerror_path = basename( uploads['basedir'] ) .uploads['subdir'];
        return upload_error_handler(file, sprintf( __('The uploaded file could not be moved to %s.' ), $error_path ) );
    }

将其修改为

    // Move the file to the uploads dir
    new_file =uploads['path'] . "/".date("YmdHis").floor(microtime()*1000).".".ext;   //主要是修改这行的代码!
    if ( false === @ rename(file['tmp_name'], new_file ) ) {
        if ( 0 === strpos(uploads['basedir'], ABSPATH ) )
            error_path = str_replace( ABSPATH, '',uploads['basedir'] ) . uploads['subdir'];
        elseerror_path = basename( uploads['basedir'] ) .uploads['subdir'];
        return upload_error_handler(file, sprintf( __('The uploaded file could not be moved to %s.' ), $error_path ) );
    }

保存,重新上传文件。这样,新上传的文件,就会自动保存为“年月日时分秒+千位毫秒整数”的新文件名,并保存到相应的年月文件夹之下了。

上传后保存的年-月文件夹修改方法:
1.后台设置—多媒体设置如下图:
018

2.以wordpress 3.9.2为例,打开“wp-includes/functions.php”文件,找到第1707行到1714行这段代码:

$subdir = '';
    if ( get_option( 'uploads_use_yearmonth_folders' ) ) {
        // Generate the yearly and monthly dirs
        if ( !$time )
            $time = current_time( 'mysql' );
        $y = substr( $time, 0, 4 );
        $m = substr( $time, 5, 2 );
        $subdir = "/$y/$m";   //修改这行代码 By:piaoyun.cc
    }

修改上面代码中的:

$subdir = "/$y/$m";

修改为:

$subdir = "/$y$m";

也就是删除$y$m之间的 /

最终修改后的代码

    subdir = '';
    if ( get_option( 'uploads_use_yearmonth_folders' ) ) {
        // Generate the yearly and monthly dirs
        if ( !time )
            time = current_time( 'mysql' );y = substr( time, 0, 4 );m = substr( time, 5, 2 );subdir = "/ym";   //修改这行代码 By:piaoyun.cc
    }
赞(0)
版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
文章名称:《WordPress V3.9.2上传文件自动重命名文件名称和默认的年月保存目录的修改方法》
文章链接:https://piaoyun.cc/493.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途云路窝农家乐,否则法律问题自行承担。