前言: 在解决问题时,我也检索了网上相关解决办法。
其中有一个是修改file.php的两处
a 、
$new_file = $uploads['path'] . "/$filename"; // 把上一行换为下一行,解决上传文件中文乱码问题 $new_file = $uploads['path'] ."/".iconv("UTF-8","GBK",$filename);
b、修改return apply_filters( 'wp_handle_upload', array(
函数
但是,事实证明,这个方法有漏洞,一个是上传文件的title是中文的urlencode编码,
一个是上传的中文附件显示删除成功,但服务器的文件没有删除
最后咨询了公司的张大神,他给出以下解决方案,很好用!
1、在themes下的function.php加入以下代码意思是文件重命名,在文件系统里保存的文件名是随机字符串。
/*
*====================================================
*wp-admin 后台上传文件重命名附件
* ====================================================
*/
function make_filename_hash($filename) {
$info = pathinfo($filename);
$ext = empty($info['extension']) ? '' : '.' . $info['extension'];
$name = basename($filename, $ext);
return date("Ymd").'_'.substr(md5($name.date("His")), 0,10).$ext;
}
add_filter('sanitize_file_name', 'make_filename_hash', 10);
2、修改保存以下文件,把title中中文乱码 用urlDecoder解码。
E:\apache24_hotdocs\wordpress\wp-admin\includes\media.php
大约365行
// Construct the attachment array
$attachment = array_merge( array(
'post_mime_type' => $type,
'guid' => $url,
'post_parent' => $post_id,
// @author jiabaochina 2016年10月11日17:03:11 上传文件的title url解码
'post_title' => urldecode($title),
'post_content' => $content,
'post_excerpt' => $excerpt,
), $post_data );