欢迎光临
我们一直在努力

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 = "/$y$m";   //修改这行代码 By:piaoyun.cc
    }
赞(0)
未经允许不得转载:飘云 » WordPress V3.9.2上传文件自动重命名文件名称和默认的年月保存目录的修改方法
分享到: 更多 (0)