wordpress文件上传部分中文字符乱码解决方案
2012-03-29 15:44
537 查看
Server Windows xp系统,Apache,PHP,MySQL,wordpress 3.3.1上传中文文件名文件:
错误信息:Warning:touch()[function.touch]: Unable to create file ...because invalid argument
网上的解决方法大都是通过修改内核。
本文我们通过插件的方式来解决这个问题。
代码如下:
错误信息:Warning:touch()[function.touch]: Unable to create file ...because invalid argument
网上的解决方法大都是通过修改内核。
本文我们通过插件的方式来解决这个问题。
代码如下:
<?php /* Plugin Name: Allow UTF-8 Filenames on Windows Plugin URI: http://core.trac.wordpress.org/ticket/15955 Description: A workaround for correct uploading of files with UTF-8 names on Windows systems. Version: 0.1 Author: Sergey Biryukov Author URI: http://profiles.wordpress.org/sergeybiryukov/ */ function autfw_sanitize_file_name_for_windows($filename, $utf8 = false) { if ( seems_utf8($filename) == $utf8 ) return $filename; // On Windows platforms, PHP will mangle non-ASCII characters, see http://bugs.php.net/bug.php?id=47096 if ( 'WIN' == substr( PHP_OS, 0, 3 ) ) { $codepage = 'Windows-' . trim( strstr( setlocale( LC_CTYPE, 0 ), '.' ), '.' ); if ( function_exists( 'iconv' ) ) { if ( false == $utf8 ) $filename = iconv( get_option( 'blog_charset' ), $codepage . '//IGNORE', $filename ); else $filename = iconv( $codepage, get_option( 'blog_charset' ), $filename ); } elseif ( function_exists( 'mb_convert_encoding' ) ) { if ( false == $utf8 ) $filename = mb_convert_encoding( $filename, $codepage, get_option( 'blog_charset' ) ); else $filename = mb_convert_encoding( $filename, get_option( 'blog_charset' ), $codepage ); } } return $filename; } add_filter('wp_delete_file', 'autfw_sanitize_file_name_for_windows'); add_filter('get_attached_file', 'autfw_sanitize_file_name_for_windows'); function autfw_handle_upload_input($file) { $file['name'] = autfw_sanitize_file_name_for_windows($file['name']); return $file; } add_filter('wp_handle_upload_prefilter', 'autfw_handle_upload_input'); function autfw_handle_upload($file) { $file['file'] = autfw_sanitize_file_name_for_windows($file['file'], true); $file['url'] = autfw_sanitize_file_name_for_windows($file['url'], true); return $file; } add_filter('wp_handle_upload', 'autfw_handle_upload'); function autfw_update_attached_file($file) { return autfw_sanitize_file_name_for_windows($file, true); } add_filter('update_attached_file', 'autfw_update_attached_file'); function autfw_generate_attachment_metadata($metadata, $attachment_id) { $file = get_attached_file($attachment_id); remove_filter('wp_generate_attachment_metadata', 'autfw_generate_attachment_metadata'); $metadata = wp_generate_attachment_metadata($attachment_id, $file); return autfw_update_attachment_metadata($metadata); } add_filter('wp_generate_attachment_metadata', 'autfw_generate_attachment_metadata', 10, 2); function autfw_update_attachment_metadata($metadata) { $metadata['file'] = autfw_sanitize_file_name_for_windows($metadata['file'], true); if ( !empty($metadata['sizes']) ) { foreach ( (array) $metadata['sizes'] as $size => $resized ) { $resized['file'] = autfw_sanitize_file_name_for_windows($resized['file'], true); $metadata['sizes'][$size] = $resized; } } return $metadata; } add_filter('wp_update_attachment_metadata', 'autfw_update_attachment_metadata'); function autfw_update_post_metadata($foo, $object_id, $meta_key, $meta_value) { if ( '_wp_attachment_backup_sizes' != $meta_key ) return null; foreach ( (array) $meta_value as $size => $resized ) { $resized['file'] = autfw_sanitize_file_name_for_windows($resized['file'], true); $meta_value[$size] = $resized; } remove_filter('update_post_metadata', 'autfw_update_post_metadata', 10, 4); update_metadata('post', $object_id, $meta_key, $meta_value); return true; } add_filter('update_post_metadata', 'autfw_update_post_metadata', 10, 4); ?>
相关文章推荐
- Struts2文件上传与下载中的中文乱码问题解决方案
- [转载]Flash新版本11.8下 上传文件时中文名显示乱码问题解决方案
- JSP或servlet中(以及上传下载文件)中文乱码或不显示的解决方案
- wordpress上传文件自动重命名的方法-原创-避免中文乱码
- wordpress4.5.4 文件上传中文名解决方案
- 上传中文文件到linux文件出现乱码问题的解决方案
- php文件上传后台存储中文乱码问题解决方案
- MultipartEntity上传文件时传递中文参数出现乱码的解决方案
- flask-admin fileadmin 上传文件,中文名的解决方案 重写部分secure_filename
- WordPress上传含有中文文件出现乱码
- [转]JSP或servlet中(以及上传下载文件)中文乱码或不显示的解决方案
- 文件上传 FileItem 中文乱码问题解决
- QT 使用QSetting读取配置文件中的中文乱码解决方案
- 中文乱码问题(使用Servlet3.0新特性实现文件上传——上传文件名中文乱码问题)
- MySQL字符集中文乱码终极解决方案和mysql查询中文问题解决方法[转贴]
- PrimeFaces <p:fileUpload>上传文件 中文乱码
- struts2解决文件上传下载中文乱码问题
- Unity3D脚本中文字符乱码的解决方案
- Oracle导入sql文件数据,中文乱码问题解决方案
- Uploadify上传文件中文文件名乱码 Java