WordPress上传中文文件名或图片名乱码问题
首先修改一下上传,打开 wp-admin/include/file.php文件,找到
改为:
首先修改一下上传,打开 wp-admin/include/file.php文件,找到
$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' );
这样就将上传的文件的文件名转换成了GBK编码,服务器就能够正确的认识文件名了。
注:Wordpress对中文支持不是很好,特别是那些插件,很多都没考虑中文因素,所以上传最好还是用英文或数字名称吧
另一种办法:
打开 wp-admin/include/file.php文件,把
// 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'] ) );