使用了Wordpress從2到4版本了,都一直沒解決的問題就是中文檔案在Windows Server底下會出現亂碼,因此為大家整理了我個人嘗試過的方法~~
方法一 (强烈建议方法一 ,以后网站搬家不用担心中文档案名称出现乱码)
修改file.php文件实现wordpress上传附件图片按照时间命名
找到“wp-admin/includes/file.php”文件,将文件中
- // Move the file to the uploads dir.
- 代码 覆盖在这个区段里面
- // Set correct file permissions.
$new_file = $uploads['path'] . "/".date("YmdHis").floor(microtime()*1000).".".$ext; if ( false === @ move_uploaded_file( $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 ) ); }
于是就实现了wordpress上传附件图片按照时间命名,时间格式为年月日小时千分秒。
注意:由于file.php处于程序管理目录中,wordpress程序升级将会影响到该修改
方法二
修改functions.php文件实现wordpress上传附件图片按照时间命名
找到你正在使用主题的functions.php文件,在文件末尾加上
function iqktv_wp_handle_upload_prefilter($file){ $time=date("Ymdhis"); $file['name'] = $time."".mt_rand(1,1000).".".pathinfo($file['name'] , PATHINFO_EXTENSION); return $file; } add_filter('wp_handle_upload_prefilter', 'iqktv_wp_handle_upload_prefilter');
方法三
找到/wp-admin/includes/file.php这个文件,并最如下修改:
// Move the file to the uploads dir //$new_file = $uploads['path'] . “/$filename”; // 修正中文文件名编码问题 $new_file = $uploads['path'] . “/” . iconv(“UTF-8″,”GBK″,$filename); //… //return apply_filters( ‘wp_handle_upload’, array( ‘file’ => $new_file, ‘url’ => $url, ‘type’ => $type ), ‘upload’ );
// 修正中文文件名编码问题
return apply_filters( ‘wp_handle_upload’, array( ‘file’ => $uploads['path'] . “/$filename”, ‘url’ => $url, ‘type’ => $type ) , ‘upload’);
注意:所有方法均需对相应文件先做备份后再进行修改!!