wordpress文件上传部分中文字符乱码解决方案


Server Windows xp系统,Apache,PHP,MySQL,wordpress 3.3.1上传中文文件名文件:
错误信息: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);
?>

posted on 2012-03-29 15:44  IT技术畅销书  阅读(383)  评论(0编辑  收藏  举报

导航