可能會有不少朋友碰到在使用WordPress上傳文件中文文件時會出現亂碼情況了,下面我來介紹中文亂碼的解決方法,這里我們是自動重命名來解決些問題.
下面以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'] ) );
保存,重新上傳文件,這樣,新上傳的文件,就會自動保存為“年月日時分秒+千位毫秒整數”的新文件名,并保存到相應的年月文件夾之下了,沒錯,就這么簡單,測試、通過,面對歐美客戶的英文外貿網站推薦使用此法.
有些wordpress我們并不不需要在file.php文件上操作,只需要在functions.php文件中操作即可,此方法更方便,在functions.php中加入以下代碼:
- function new_filename($filename) {
- $info = pathinfo($filename);
- $ext = emptyempty($info['extension']) ? '' : '.' . $info['extension'];
- $name = basename($filename, $ext);
- return md5($name) . $ext;
- }
- add_filter('sanitize_file_name', 'new_filename', 10);
添加保存之后,就可以實現了文件的自動更名,自動生成的是一個32個的md5加密的文件名,如果你認識32位的文件名太長了,你可以使用 substr()來截取你想要的長度,代碼如下,我使用的是15位,代碼如下:
- function new_filename($filename) {
- $info = pathinfo($filename);
- $ext = emptyempty($info['extension']) ? '' : '.' . $info['extension'];
- $name = basename($filename, $ext);
- return substr(md5($name), 0, 15) . $ext;
- }
- add_filter('sanitize_file_name', 'new_filename', 10);
新聞熱點
疑難解答
圖片精選