中文附件乱码原因与其它php上传中文名乱码原因是一样的就是编码问题,因为php对中文支持不怎么样我可以转换编码或直接重命令上传的文件名即可.
熟悉PHP的朋友可能会很快解决问题,但不熟悉PHP代码的朋友看过此文章相信一定能解决你的问题,接下来就分享下我的解决wordpress上传中文文件名乱码的心得吧~:
找到/wp-admin/includes/file.php这个文件,并最如下修改:
- function wp_handle_upload( &$file, $overrides = false, $time = null ) {
- //….
- // Move the file to the uploads dir
- //$new_file = $uploads['path'] . “/$filename”;
- // 修正中文文件名编码问题
- $new_file = $uploads['path'] . “/” . iconv(“UTF-8″,”GB2312″,$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’);
修改完上传至服务器,问题就解决啦,其实很简单的啦,简单的,把以下代码添加到主题目录functions.php 文件:
- function upload_file($filename) {
- $parts = explode(‘.’, $filename);
- $filename = array_shift($parts);
- $extension = array_pop($parts);
- foreach ( (array) $parts as $part)
- $filename .= ‘.’ . $part;
- if(preg_match(‘/[一-?]/u’, $filename)){
- $filename = md5($filename);
- }
- $filename .= ‘.’ . $extension;
- return $filename ;
- }
- add_filter(‘sanitize_file_name’, ‘upload_file’, 5,1);
上传文件自动重命名的方法,下面以wordpress 3.2.1为例,打开wp-admin/includes/file.php文件,找到第326行这段代码:
- // Move the file to the uploads dir
- $new_file = $uploads['path'] . "/$filename";
- if ( false === @ move_uploaded_file( $file['tmp_name'], $new_file ) )
- return $upload_error_handler( $file, sprintf( __('The uploaded file could not be moved to %s.' ), $uploads['path'] ) );
将其修改为:
- // Move the file to the uploads dir
- $new_file = $uploads['path'] . "/".date_i18n("YmdHis").floor(microtime()*1000).".".$ext;
- if ( false === @ move_uploaded_file( $file['tmp_name'], $new_file ) )
- return $upload_error_handler( $file, sprintf( __('The uploaded file could not be moved to %s.' ), $uploads['path'] ) );
保存,重新上传文件,这样,新上传的文件,就会自动保存为“年月日时分秒+千位毫秒整数”的新文件名,并保存到相应的年月文件夹之下了,没错,就这么简单,测试、通过,面对欧美客户的英文外贸网站推荐使用此法.
新闻热点
疑难解答
图片精选