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

WordPress V3.9.2上传文件自动重命名文件名称和默认的年月保存目录的修改方法 -云主机博士 第1张

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 ) );
    }

PHP

将其修改为

    // 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 ) );
    }

PHP

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

上传后保存的年-月文件夹修改方法:

    后台设置—多媒体设置如下图:

    以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:yunzhujiboshi.com
        }

    PHP

    修改上面代码中的:

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

    PHP

    修改为:

    $subdir = "/$y$m";

    PHP

    也就是删除$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:yunzhujiboshi.com
        }


宝塔服务器面板,一键全能部署及管理,送你3188元礼包,点我领取


iproyal.png
原文链接:,转发请注明来源!
「WordPress V3.9.2上传文件自动重命名文件名称和默认的年月保存目录的修改方法 -云主机博士」评论列表

发表评论