Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2561381
  • 博文数量: 245
  • 博客积分: 4125
  • 博客等级: 上校
  • 技术积分: 3113
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-25 23:56
文章分类

全部博文(245)

文章存档

2015年(2)

2014年(26)

2013年(41)

2012年(40)

2011年(134)

2010年(2)

分类: 系统运维

2012-04-26 12:43:07

Server Windows xp系统,Apache,PHP,MySQL,wordpress 3.3.1上传中文文件名文件:
错误信息:Warning:touch()[function.touch]: Unable to create file …because invalid argument
网上的解决方法大都是通过修改内核。
本文我们通过插件的方式来解决这个问题。
代码如下:


点击(此处)折叠或打开

  1. <?php
  2. /*
  3. Plugin Name: Allow UTF-8 Filenames on Windows
  4. Plugin URI: wordpress.org/ticket/15955
  5. Description: A workaround for correct uploading of files with UTF-8 names on Windows systems.
  6. Version: 0.1
  7. Author: Sergey Biryukov
  8. Author URI: wordpress.org/sergeybiryukov/
  9. */
  10.  
  11. function autfw_sanitize_file_name_for_windows($filename, $utf8 = false) {
  12.     if ( seems_utf8($filename) == $utf8 )
  13.         return $filename;
  14.  
  15.     // On Windows platforms, PHP will mangle non-ASCII characters, see
  16.     if ( 'WIN' == substr( PHP_OS, 0, 3 ) ) {
  17.         $codepage = 'Windows-' . trim( strstr( setlocale( LC_CTYPE, 0 ), '.' ), '.' );
  18.         if ( function_exists( 'iconv' ) ) {
  19.             if ( false == $utf8 )
  20.                 $filename = iconv( get_option( 'blog_charset' ), $codepage . '//IGNORE', $filename );
  21.             else
  22.                 $filename = iconv( $codepage, get_option( 'blog_charset' ), $filename );
  23.         } elseif ( function_exists( 'mb_convert_encoding' ) ) {
  24.             if ( false == $utf8 )
  25.                 $filename = mb_convert_encoding( $filename, $codepage, get_option( 'blog_charset' ) );
  26.             else
  27.                 $filename = mb_convert_encoding( $filename, get_option( 'blog_charset' ), $codepage );
  28.         }
  29.     }
  30.  
  31.     return $filename;
  32. }
  33. add_filter('wp_delete_file', 'autfw_sanitize_file_name_for_windows');
  34. add_filter('get_attached_file', 'autfw_sanitize_file_name_for_windows');
  35.  
  36. function autfw_handle_upload_input($file) {
  37.     $file['name'] = autfw_sanitize_file_name_for_windows($file['name']);
  38.     return $file;
  39. }
  40. add_filter('wp_handle_upload_prefilter', 'autfw_handle_upload_input');
  41.  
  42. function autfw_handle_upload($file) {
  43.     $file['file'] = autfw_sanitize_file_name_for_windows($file['file'], true);
  44.     $file['url'] = autfw_sanitize_file_name_for_windows($file['url'], true);
  45.     return $file;
  46. }
  47. add_filter('wp_handle_upload', 'autfw_handle_upload');
  48.  
  49. function autfw_update_attached_file($file) {
  50.     return autfw_sanitize_file_name_for_windows($file, true);
  51. }
  52. add_filter('update_attached_file', 'autfw_update_attached_file');
  53.  
  54. function autfw_generate_attachment_metadata($metadata, $attachment_id) {
  55.     $file = get_attached_file($attachment_id);
  56.  
  57.     remove_filter('wp_generate_attachment_metadata', 'autfw_generate_attachment_metadata');
  58.     $metadata = wp_generate_attachment_metadata($attachment_id, $file);
  59.  
  60.     return autfw_update_attachment_metadata($metadata);
  61. }
  62. add_filter('wp_generate_attachment_metadata', 'autfw_generate_attachment_metadata', 10, 2);
  63.  
  64. function autfw_update_attachment_metadata($metadata) {
  65.     $metadata['file'] = autfw_sanitize_file_name_for_windows($metadata['file'], true);
  66.  
  67.     if ( !empty($metadata['sizes']) ) {
  68.         foreach ( (array) $metadata['sizes'] as $size => $resized ) {
  69.             $resized['file'] = autfw_sanitize_file_name_for_windows($resized['file'], true);
  70.             $metadata['sizes'][$size] = $resized;
  71.         }
  72.     }
  73.  
  74.     return $metadata;
  75. }
  76. add_filter('wp_update_attachment_metadata', 'autfw_update_attachment_metadata');
  77.  
  78. function autfw_update_post_metadata($foo, $object_id, $meta_key, $meta_value) {
  79.     if ( '_wp_attachment_backup_sizes' != $meta_key )
  80.         return null;
  81.  
  82.     foreach ( (array) $meta_value as $size => $resized ) {
  83.         $resized['file'] = autfw_sanitize_file_name_for_windows($resized['file'], true);
  84.         $meta_value[$size] = $resized;
  85.     }
  86.  
  87.     remove_filter('update_post_metadata', 'autfw_update_post_metadata', 10, 4);
  88.     update_metadata('post', $object_id, $meta_key, $meta_value);
  89.  
  90.     return true;
  91. }
  92. add_filter('update_post_metadata', 'autfw_update_post_metadata', 10, 4);
  93. ?>



阅读(3126) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~