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'];
		else
			$error_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'];
		else
			$error_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
	}
暂无评论